Re: NSDictionary, allKeys and the NSAutoreleasePool
Re: NSDictionary, allKeys and the NSAutoreleasePool
- Subject: Re: NSDictionary, allKeys and the NSAutoreleasePool
- From: Scott Andrew <email@hidden>
- Date: Thu, 3 Sep 2009 07:56:37 -0700
"ar" is not a member of "d"i. allKeys creates an autoreleased NSArray
with the keys values retained in indexes. "ar" is placed in the auto
release pool when it is created in the allKeys call.
When you call [autorelease release] the items are freed when you since
the pool is released and freed. By the time you call [ar release] the
items no longer exist. As Cocoa documentation states all items
returned from a message are autoreleased unless otherwise stated in
the documentation for the API call.
Scott
On Sep 3, 2009, at 7:21 AM, Horst Jäger wrote:
Hi,
yesterday I stumbled upon something strange concerning the
autorelease pool.
Please consinder the following lines of code:
NSLog(@"devel");
NSAutoreleasePool *autoreleasePool = [[NSAutoreleasePool alloc]
init];
NSDictionary *di = [[NSDictionary alloc] init];
NSArray *ar = [di allKeys];
// if ar were a member of di, it would be deallocated here
[di release];
// if ar were a constructed and autoreleased, it would be
deallocated here
[autoreleasePool release];
// but it is not; it is still present
NSLog(@"ar %i ", [ar retainCount]);
// adding the following line leads to a crash
//[ar release];
NSLog(@"/devel");
They yield:
[Session started at 2009-09-03 13:32:56 +0200.]
2009-09-03 13:32:58.837 KalaHoTi[3180:207] devel
2009-09-03 13:32:58.841 KalaHoTi[3180:207] ar 1
2009-09-03 13:32:58.842 KalaHoTi[3180:207] ar 1
2009-09-03 13:32:58.842 KalaHoTi[3180:207] /devel
I dont't unserstand, how ar mgiht be released:
1. If [ar autorelease] were called upon it in nthe process of
creation (and so the name "allKeys"
suggests), it were released as soon as the embracing autoreleasepool
is. But it is apparently not.
2. If ar were a member of di, it would be deallocated when di is
releaed. Apparenly, that isn't the
case either.
3. But if you try to release ar yourself, the program crashes.
Any explanation?
Thanks in advance
Horst
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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