• 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: Newbie Memory Questions on a Code Fragment
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie Memory Questions on a Code Fragment


  • Subject: Re: Newbie Memory Questions on a Code Fragment
  • From: mmalc Crawford <email@hidden>
  • Date: Wed, 21 Jan 2009 10:14:33 -0800


On Jan 21, 2009, at 9:27 AM, Kenneth Bruno II wrote:

On Jan 21, 2009, at 12:04 PM, mmalc Crawford wrote:
When I no longer need it I'll just call release on the object, this would most likely be done in the dealloc method of the class in which I initialized my object.
As a general statement, this is at best misleading.
If you maintain an object as an instance variable, you're expected to provide accessor methods to retain and release it as well.
Not necessarily, the object could be purely for internal use in the class in which case you wouldn't provide accessor methods.

On the contrary, even if the object is purely for internal use, you should typically provide accessor methods.
You might not make them public, but using accessor methods pervasively is the best first step to getting memory management right (especially for a beginner...).


You should only call release on objects that you have created through methods that begin with alloc, new, or copy or objects that you have previously called retain upon. I suggest that you review the Cocoa Memory Management Rules to understand this further:
<http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/MemoryManagementRules.html >
If you review that, you will find that your summary contains a small but important omission. This is why it's typically better simply to point to the Guide than to try to summarise yourself.
Which is exactly why I linked to it and suggested that he read it instead of just relying on my summary.

So why post an inaccurate summary in the first place?
Please, as I've said before, I'm increasingly convinced that most newcomers' confusion with memory management stems from inaccurate and misleading advice from forums and the web, so just point to the documentation.



One small error here, I should have added this line after these two in order to properly release the NSDateFormatter:
[aFormatter release];
This needs to be done because I created a new NSDateFormatter which has a retain count of 1.
"Which you don't own..."
Chasing retain counts is one of the surest ways of quickly becoming confused when trying to track down memory management problems.
I wouldn't ever advocate chasing retain counts, I was simply pointing out the fact that the object will stay around because I retained it. I probably should have said "has been retained by me and must be released" or something similar.

Yes.

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


References: 
 >Newbie Memory Questions on a Code Fragment (From: James Cicenia <email@hidden>)
 >Re: Newbie Memory Questions on a Code Fragment (From: Kenneth Bruno II <email@hidden>)
 >Re: Newbie Memory Questions on a Code Fragment (From: mmalc Crawford <email@hidden>)
 >Re: Newbie Memory Questions on a Code Fragment (From: Kenneth Bruno II <email@hidden>)

  • Prev by Date: access programmatically the quartz composer image filters?
  • Next by Date: Re: Observing [NSWindow orderOut:] ?
  • Previous by thread: Re: Newbie Memory Questions on a Code Fragment
  • Next by thread: Re: Newbie Memory Questions on a Code Fragment
  • Index(es):
    • Date
    • Thread