Re: Releasing what's in an array?
Re: Releasing what's in an array?
- Subject: Re: Releasing what's in an array?
- From: Shawn Erickson <email@hidden>
- Date: Fri, 25 Feb 2005 11:39:23 -0800
On Feb 25, 2005, at 11:20 AM, Mark Dawson wrote:
Does this mean that you have to do extra work if you release an array
with objects that should NOT be released by you (i.e., objects you did
NOT allocate)? A "more work" case would be an array of strings, some
of which you allocated (and thus you should release), and some of
which you got via getters (ones that you don't release). With
something like that, would you have to keep track of what was
allocated and what wasn't? Then iterate through the array, increasing
the retain count for those that shouldn't be released (thus having
leaving the retain count what it should be, after NSArray's dealloc
releases everything)?
I think you are misunderstanding memory management in Cocoa and likely
what release means... it is really simple once it clicks.
The short answer is no you do not need to track things such as you are
worried about in the above.
Longer answer...
Very important point to drive home is that release != dealloc. Release
only means that one less person, etc. is interested in the object and
once no one cares about the object it will be released (retain count
reaches zero). So be careful when you say release or think release to
insure you don't actually mean dealloc or vice versa.
Your code is not NSArray's code so what NSArray does to honor the
memory contract doesn't really affect what your code does to respect
the same contract.
So put yourself in NSArray's shoes for a moment... given the fact that
NSArray needs to hold onto the object it references is must retain them
when added (the intent of add is to have the object available via the
array and if it didn't retain them then the object could disappear out
from under the array) and knowing that the Cocoa memory contract states
"release what you retained or allocated (i.e. implicit retain) when you
are done with it" then NSArray must insure that release is sent to
balance the retain it did when the object was added. In other words it
increments the retain count by one on add and then later decrements the
retain count by one when the array doesn't care about the object any
more (removed or the array itself was deallocated).
It is unimportant from the point of view of NSArray if the object you
are putting into it came from another method like a getter or an object
that you yourself allocated. Regardless of how the object exists
NSArray does what it needs to do to insure that it lives up to the
memory contract and the intent of its implementation (make an added
object available in the future from the array).
Now it is important for your code to honor the memory contract. So if
you get an object from a getter and you send it a retain then you must
send it a release or autorelease to balance that retain. The same is
true if you allocated the object (alloc, copy, etc.), you must balance
the implicit retain with a release or autorelease.
You honor the contract and NSArray honors the contract, etc. so you
don't have to track who should do what since the contract is clear on
who should do what and when.
Anyway review Apple's documentation on memory management in Cocoa if
you haven't already done so...
<http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/
index.html>
-Shawn
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden