On 05/02/2009, at 4:20 PM, Chris Suter wrote:

On Thu, Feb 5, 2009 at 4:10 PM, Kiel Gillard <kiel.gill...@gmail.com> 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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to