Re: Memory management question in Objective-C 2.0 @property notation
Re: Memory management question in Objective-C 2.0 @property notation
- Subject: Re: Memory management question in Objective-C 2.0 @property notation
- From: Kiel Gillard <email@hidden>
- Date: Fri, 6 Feb 2009 08:40:54 +1100
Thanks mmalc, that clears things up for me.
On 05/02/2009, at 8:08 PM, mmalc Crawford wrote:
On Feb 4, 2009, at 10:01 PM, Kiel Gillard wrote:
I'm confused as to why else the memory would be leaking? Can you
please identify my error?
The error is in your explanation.
However, doing this will yield a memory leak:
self.name = [[NSString alloc] init];
...because the property definition tells the compiler the methods
it synthesizes should retain the value.
This is not a memory leak "because the property definition tells the
compiler the methods it synthesizes should retain the value"; it is
a leak because you're not abiding by the memory management rules.
You're creating an object you own (alloc), and not relinquishing
ownership.
(See <http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/MemoryManagementRules.html
> for full details.)
There are several ways to remedy the problem; the overall best
practice approach is:
NSString *aString = [[NSString alloc] init];
self.name = aString;
[aString release];
I suggest that the code quoted above will yield a memory leak
because the NSString instance allocated will have a retain count of
two
Explaining memory management at this level in terms of retain counts
is a leap down the wrong path.
mmalc
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden