的參數的自動釋放對象可能會導致以下代碼出現問題嗎?Objective-C:作爲方法
- (void) method1 {
NSMutableArray *myArray = [[[NSMutableArray alloc] init] autorelease];
... fill the array
[someObject someMethod:myArray]; // someObject does work on that array (without retain!)
}
一個在我的應用程序有時是出現暴跌看起來這是一個問題;但我不明白......不應該讓myArray至少在method1結束之前保持活躍狀態?
非常感謝您的幫助!
所以我的問題除了「這可能是一個問題」,是: - 是否足以移除autorelease,並在方法結束時發佈? - 如果不是:我必須在「someMethod」中保留/釋放嗎?
編輯:但這可能是一個問題,我對嗎?
- (void) method1 {
NSMutableArray *myArray = [self getArray];
... fill the array
[someObject someMethod:myArray]; // someObject does work on that array (without retain!)
}
- (NSMutableArray) method2 {
return [[[NSMutableArray alloc] init] autorelease];
}
Cocoa的所有權規則指定接收的對象應該在調用方法的整個範圍內保持有效,除非您執行以下兩項操作之一:(1)從集合中刪除對象或(2)釋放「父對象」。請參閱[避免導致您正在使用的對象的釋放](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/ 20000043-1000922) – albertamg