开发者

NSMutableArray property - Release children

Here is what I see when I run the following code:

// 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];

Why does dealloc not get called in the first example?

EDIT:

I think I was misunderstanding all this. Basically what I need to do is:

NSMutableArray* alocal 开发者_Python百科= [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

Just as a note, with the autorelease/array options (#2 and #3), a does not get released right away, so if you want more control go with option #1.

Thanks!


Because in the first example, assuming a is a retain/copy property, the array leaks.

self.a = [NSMutableArray new];

which, by the way, is different from the third example:

a = [NSMutableArray new];

In the third case, the object is assigned directly to the ivar (no property setter involved).

Returning to the first example, the method new returns a retained object (+1) and then the property setter retains as well the object (+2). Then, when the property is released (-1), you end up with a +1. This is wrong. You need to balance your retain/releases to end up with a +0.

A simple solution is to use a convenience constructor:

self.a = [NSMutableArray array];

In this case, the method array returns an autoreleased object (+0) and the property setter retains the object (+1), which is balanced by the release of the property (-1) and you end up with a +0.


Also, in the first example, you release the object you set to the property through the ivar:

[a release];

As a result, the property is now holding a pointer to a potentially deallocated object. Since you relinquished ownership of the object, there is no guarantee that the object will be alive in the future. If the object was in fact deallocated (because it doesn't have any other owners), your pointer would still point to the memory address in the heap where the object used to live; except that now, that address might contain anything: the old bit pattern of the said object, or an unrelated object, or absolute garbage.

Nothing good can happen if you send a message to the deallocated object, e.g. it will make your app crash. To avoid this, release the object through the property:

self.a = nil;


It depends on how you implemented the property 'a' It should be:

@property (retain) NSMutableArray *a;

also when using 'new' it is putting an allocation so you should autorelease it like this:

self.a = [[NSMutableArray new] autorelease];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜