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
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 mails | Author | Date |
|---|---|---|
| Avery Prometheus R… | Dec 19, 23:20 | |
| Ingvar Nedrebo | Dec 20, 03:27 | |
| Ian P. Cardenas | Dec 20, 05:47 | |
| Erik J. Barzeski | Dec 20, 06:30 | |
| Avi Cherry | Dec 20, 06:53 | |
| Geoff Coffey | Dec 20, 07:07 | |
| Ingvar Nedrebo | Dec 20, 16:30 | |
| Marcel Weiher | Jan 3, 07:00 |






Cocoa mail archive

