Re: NSEnumerator retaining objects?
Re: NSEnumerator retaining objects?
- Subject: Re: NSEnumerator retaining objects?
- From: Chris Kane <email@hidden>
- Date: Wed, 9 Mar 2005 11:46:56 -0800
On Mar 9, 2005, at 8:12 AM, James Bucanek wrote:
Marco Scheurer wrote on Wednesday, March 9, 2005:
I checked with the version of frameworks I'm using, and sad but true,
objects are retained by nextObject!
Thanks for the confirmation. Can you also easily confirm if the
NSEnumerator retains the collection? (I realize that this will be
on a case-by-case basis, as NSEnumerator is just the abstract
superclass and each collection will implement its own
NSEnumerator. But I'd be interested mostly in the cases of NSArray
and NSMutableArray.)
I have a few comments:
1) Insofar as NSEnumerator is an abstract class, NSEnumerator doesn't
"do" anything except implement -allObjects to return an array of the
remaining objects in the enumeration and exhaust the enumeration.
2) The enumerator subclass used by NSArray, NSDictionary, and NSSet
does retain the collection during enumeration. As shown by Marco's
recent example, when the enumeration is exhausted, the collection is
released.
3) The enumerator class does NOT retain/autorelease every object
returned from -nextObject. Marco's test case was too trivial to
observe the real behavior: only the last object from the enumeration
is retained/autoreleased, as -nextObject returns the last object. In
Marco's test, there was only one object in the collection, so it was
the one that got retained/autoreleased. The retain/autorelease is
required because the collection is released in the same call, so the
object could go away if the collection held the only retain.
This is the primordial behavior (as far back as CVS records go),
about 8.5 years, probably older.
There would be two other reasonable behaviors for the enumerator
(that I can immediately think of) with respect to the retain on the
collection. I leave it to the reader to discover them and ponder the
implications of each.
Chris Kane
Cocoa Frameworks, Apple
_______________________________________________
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