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: Thu, 5 Feb 2009 17:01:36 +1100
On 05/02/2009, at 4:20 PM, Chris Suter wrote:
On Thu, Feb 5, 2009 at 4:10 PM, Kiel Gillard
<email@hidden> wrote:
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.
You're right that it will leak in that case but you've given the wrong
reason as to why. Memory management rules are covered by Apple's
documentation.
Regards,
Chris
Thanks for your reply, Chris.
I suggest that the code quoted above will yield a memory leak because
the NSString instance allocated will have a retain count of two after
the setName: message has be sent to self. To correct this error, I
suggest that the code should read:
self.name = [[[NSString alloc] init] autorelease];
Under the heading of "Setter Semantics" of <http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_5_section_3.html#//apple_ref/doc/uid/TP30001163-CH17-SW2
>, I can see that Apple's documentation clearly states that the
implementation of a property declared with a retain attribute will
send a retain message to the value given in the right hand side of the
assignment.
I'm confused as to why else the memory would be leaking? Can you
please identify my error?
Thanks,
Kiel
_______________________________________________
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