Re: NSDictionary, allKeys and the NSAutoreleasePool
Re: NSDictionary, allKeys and the NSAutoreleasePool
- Subject: Re: NSDictionary, allKeys and the NSAutoreleasePool
- From: Roland King <email@hidden>
- Date: Thu, 3 Sep 2009 22:28:34 +0800
Is that *all* the code? Your output has two 'ar 1' lines but you only
NSLog() with that pattern once. The output doesn't match the code.
On 03-Sep-2009, at 10:21 PM, 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