"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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/scottandrew%40roadrunner.com

This email sent to scottand...@roadrunner.com

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to