Re: NSEnumerator retaining objects?
Re: NSEnumerator retaining objects?
- Subject: Re: NSEnumerator retaining objects?
- From: James Bucanek <email@hidden>
- Date: Wed, 9 Mar 2005 09:12:54 -0700
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.)
>This is IMHO a perfect example of the problems incurred by the
>"autorelease accessors" school of thought. Everybody realizes what a
>waste and potential performance issue it is, but we may have to stick
>with that because some brain dead code may depends on this "feature".
I agree. If, as is documented, NSEnumerator shouldn't be used on a mutable collection that is being modified between calls to nextObject, then the retain/autorelease of the each object in the collection is completely redundant.
Unless you dispose of the collection (and that could be prevented if NSEnumerator retained the collection during its useful lifetime), the collection already retains all of the objects in it. So the only reason to retain an object returned by nextObject would be to prevent the following code from failing:
id o;
NSEnumerator e = [array objectEnumerator];
while ( (o=[e nextObject])!=nil )
{
[array removeObject:o];
[o doSomething];
}
But, as has already been pointed out, you should never remove an object from an array while iterating through it. And if you plan to do something with an object that you're going to remove from a collection, you should retain it first. So the above code is already suspect and deserves to break, IMNSHO.
Unless anyone else can present a counter example, I think I'll file a bug on this too.
--
James Bucanek <mailto:email@hidden>
_______________________________________________
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