• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Memory management question in Objective-C 2.0 @property notation
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory management question in Objective-C 2.0 @property notation


  • Subject: Re: Memory management question in Objective-C 2.0 @property notation
  • From: Bryan Henry <email@hidden>
  • Date: Thu, 05 Feb 2009 10:53:23 -0500

No, you do need to release it. You should release the ivars for any retain or copy-type properties in your -dealloc implementation. Is your property was just an assign property, then you would not need to release it in -dealloc.

Also - a common mistake when using properties is to set the ivar, but not use the property setter.

This:						name = [NSString string];
is very different from this:		self.name = [NSString string];
as the latter is equivalent to:	[self setName:[NSString string]];

Bryan

P.S. Why isn't email@hidden set as the Reply-To header for emails to the list? I know other people must make the mistake of hitting Reply instead of Reply All.

On Feb 5, 2009, at 3:21 AM, Devraj Mukherjee wrote:

Thanks again both of you.

assuming that I do self.name = [NSString string] and since it the
NSString helper message, I shouldn't have to release that in my
dealloc implementation.

Or am I understanding this incorrectly.

On Thu, Feb 5, 2009 at 5:01 PM, Kiel Gillard <email@hidden> wrote:
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





-- "The secret impresses no-one, the trick you use it for is everything" - Alfred Borden (The Prestiege) _______________________________________________

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


References: 
 >Memory management question in Objective-C 2.0 @property notation (From: Devraj Mukherjee <email@hidden>)
 >Re: Memory management question in Objective-C 2.0 @property notation (From: Kiel Gillard <email@hidden>)
 >Re: Memory management question in Objective-C 2.0 @property notation (From: Chris Suter <email@hidden>)
 >Re: Memory management question in Objective-C 2.0 @property notation (From: Kiel Gillard <email@hidden>)
 >Re: Memory management question in Objective-C 2.0 @property notation (From: Devraj Mukherjee <email@hidden>)

  • Prev by Date: Re: Interface Builder Nested Object Selection
  • Next by Date: [Q] How can one programatically begin a text editing session in a NSTextField?
  • Previous by thread: Re: Memory management question in Objective-C 2.0 @property notation
  • Next by thread: Re: Memory management question in Objective-C 2.0 @property notation
  • Index(es):
    • Date
    • Thread