• 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: 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


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: mmalc Crawford <email@hidden>)

  • Prev by Date: Re: Saving application data in ~/Library/Application Support/
  • Next by Date: Re: Confused about NSTrackingAreas with autoscroll [WORKAROUND]
  • Previous by thread: Re: Memory management question in Objective-C 2.0 @property notation
  • Next by thread: Bug in QCRenderer ...?
  • Index(es):
    • Date
    • Thread