Re: About NSEnumerator
Re: About NSEnumerator
- Subject: Re: About NSEnumerator
- From: Conor <email@hidden>
- Date: Wed, 18 Apr 2007 19:41:56 +0200
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. 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.
}
P.S. The double parenthesis are not necessary in this while loop case.
Regards,
Conor
http://www.bruji.com/
_______________________________________________
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