這是我所看到的,當我運行下面的代碼:NSMutableArray的財產 - 發行兒童
// Dealloc on MyClass not called
MyClass* c = [MyClass new];
self.a = [NSMutableArray new];
[self.a addObject:c];
[c release];
[a release];
// Dealloc on MyClass called
MyClass* c2 = [MyClass new];
NSMutableArray* a2 = [NSMutableArray new];
[a2 addObject:c2];
[c2 release];
[a2 release];
// Dealloc on MyClass called
MyClass* c3 = [MyClass new];
a = [NSMutableArray new];
[a addObject:c3];
[c3 release];
[a release];
爲什麼dealloc中不會被調用的第一個例子嗎?
編輯:
我想我是誤解了這一切。基本上我需要做的是:
NSMutableArray* alocal = [NSMutableArray new]; // alocal has a retain count of 1
self.a = alocal; // a has a retain count of 1
[alocal release]; // alocal has a retain count of 0
[a release]; // a has a retain count of 0
OR
self.a = [[NSMutableArray new] autorelease]; // a has a retain count of 2
[a release]; // a has a retain count of 0
OR
self.a = [NSMutableArray array]; // a has a retain count of 2
[a release]; // a has a retain count of 0
正如一個音符,與自動釋放/陣列選項(#2,#3), a不會馬上釋放,所以如果你想要更多的控制,請使用選項#1。
謝謝!
有趣的...所以基本上你希望它在那一刻被釋放吧?這聽起來很公平。通常我們只是讓游泳池自動釋放,但你是對的,它不會立即釋放。 – raixer