Given the following property definition:
@property (nonatomic,retain) MyObject* foo;
Does the following code lead to a memory leak:
self.foo = [[MyObject alloc] init];
?
It looks like the alloc call increments the hold count by 1 to the object, and then holds the properties inside the setterter. But since the initial count never decreases to 0, the object will stick even when self is released. Is this analysis correct?
If so, it looks like I have two alternatives:
self.foo = [[[MyObject alloc] init] autorelease];
which is not recommended for iPhone for performance reasons, or:
MyObject* x = [[MyObject alloc] init];
self.foo = x
[x release];
which is a little cumbersome. Are there other alternatives?
source
share