Skip navigation.
 
mlautorelease article on cocoa dev central
FROM : Avery Prometheus Rosen
DATE : Thu Dec 19 23:20:01 2002

Upon reading the article 
(http://www.cocoadevcentral.com/tutorials/
showpage.php?show=00000063.php) posted on the cocoa dev central about 
memory management, I noticed something which seems to be in 
contradiction to apple's developer docs on autorelease pools.

Here is a code sample from that article:

NSArray *myArray = [[NSArray alloc] initWithObjects:a,b,c,nil];
id myObj = [myArray objectAtIndex:1];
[myArray release];
NSLog(@"%@", myObj); // BANG! CRASH!

Let's say a, b, c, are autoreleased objects.  When myArray is 
initialized, then they are retained, and when myArray is released, then 
so are they.

According to my understanding, autoreleased objects are only sent that 
release when the pool on the autorelease stack which was topmost when 
they were sent the autorelease message is released.  This means that no 
matter how much code runs between the release of the myArray and the 
NSLog line, unless one of those lines releases an autorelease pool, 
there is *no* *chance* that that code will crash.

However, the author of the article assured me, when I wrote him, that 
what's in the Cocoa docs about autorelease pools isn't the whole story, 
and that the analysis above only applies to the pools that *you* create.

I have not been able to substantiate this anywhere... can anyone 
confirm or deny this notion?

/p


Related mailsAuthorDate
mlautorelease article on cocoa dev central Avery Prometheus R… Dec 19, 23:20
mlRe: autorelease article on cocoa dev central Ingvar Nedrebo Dec 20, 03:27
mlRe: autorelease article on cocoa dev central Ian P. Cardenas Dec 20, 05:47
mlRe: autorelease article on cocoa dev central Erik J. Barzeski Dec 20, 06:30
mlRe: autorelease article on cocoa dev central Avi Cherry Dec 20, 06:53
mlRe: autorelease article on cocoa dev central Geoff Coffey Dec 20, 07:07
mlRe: autorelease article on cocoa dev central Ingvar Nedrebo Dec 20, 16:30
mlRe: autorelease article on cocoa dev central Marcel Weiher Jan 3, 07:00