Re: NSEnumerator retaining objects?
Re: NSEnumerator retaining objects?
- Subject: Re: NSEnumerator retaining objects?
- From: Marco Scheurer <email@hidden>
- Date: Wed, 9 Mar 2005 20:00:13 +0100
On Mar 9, 2005, at 17:12, 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.)
That's right, NSSet's enumerator may behave differently. I don't have
any inside knowledge about the implementation of NSArray's enumerator,
but its effects can easily be measured using retainCount:
{
NSString *object = [NSString stringWithFormat:@"%s", "Hello"];
NSLog (@"1: object: %d", [object retainCount]);
NSArray *array = [NSArray arrayWithObject:object];
NSLog (@"2: object: %d, collection: %d", [object retainCount],
[array retainCount]);
NSEnumerator *enumerator = [array objectEnumerator];
NSLog (@"3: object: %d, collection: %d", [object retainCount],
[array retainCount]);
id each;
while (each = [enumerator nextObject]) {
NSLog (@"4: object: %d, collection: %d", [object retainCount],
[array retainCount]);
}
}
2005-03-09 19:51:29.884 Lab[8029] 1: object: 1
2005-03-09 19:51:29.885 Lab[8029] 2: object: 2, collection: 1
2005-03-09 19:51:29.885 Lab[8029] 3: object: 2, collection: 2
2005-03-09 19:51:29.885 Lab[8029] 4: object: 3, collection: 1
We can see that nextObject retains (4) the object, and that the
enumerator retains the array (3), but releases it when done (4, as
expected, there is no autorelease in that case).
Marco Scheurer
Sen:te, Lausanne, Switzerland http://www.sente.ch
_______________________________________________
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