Re: About NSEnumerator
Re: About NSEnumerator
- Subject: Re: About NSEnumerator
- From: "Shawn Erickson" <email@hidden>
- Date: Wed, 18 Apr 2007 11:18:37 -0700
On 4/18/07, Conor <email@hidden> wrote:
I. Savant is right about not having to release it. But if you can understand
how memory management works in this example then you will understand it for
all of Cocoa.
Learn the memory contract is all you need to do.
This example is one of the more complex.
NSArray *anArray = // ... ;
The array is retaining the objects inside of the array.
NSEnumerator *enumerator = [anArray objectEnumerator];
The enumerator is retaining the array, which in turn is retaining the
objects.
id object;
while ((object = [enumerator nextObject])) {
Here comes the tricky part. When you get the last object the enumerator
releases the array. If the enumerator was the last one retaining the array
(it's not in this case) then the array releases itself releasing all the
objects it was holding. In order to avoid the last object being released
with the array, before the enumerator releases the array it does a retain
followed by an autorelease on the last object. So that the last object will
at least stick around until the next autorelease pool.
I apologize as this is more than you bargained for, but understanding how
memory management is done in the built in methods can better your own code.
}
The real thing to take away from the above is that you don't have to
worry about what was just outlined above.
Learn and follow the memory contract and be happy. :)
-Shawn
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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