Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory Management



On Wednesday, July 30, 2003, at 09:25 PM, email@hidden wrote:


Am Mittwoch, 30.07.03 um 01:13 Uhr schrieb John Randolph:

On Tuesday, July 29, 2003, at 11:23 AM, Nat! wrote:
Autorelease pools have some clear semantics. Every object added to
an autorelease pool gains a property, a guaranteed minimum lifetime.

Umm, no.

An object added to an autorelease pool is going to get a -release
message whenever the pool gets released, and that's all. The act of
adding it to the pool has no effect of guaranteeing a minimum >
lifetime.

-jcr

ok, I should have explicitily stated the assumption that the object is
added retained.

But with my answer I am obscuring my own point, that adding it retained
to the pool does guarantee a minimum lifetime. As John states "An
object added to an autorelease pool is going to get a -release message
whenever the pool gets released". That's exactly the minimum lifetime.
It can be longer, but it can't be less.

And adding it non-retained and not crashing is a trick you must show me
as

s = [[NSString alloc] initWithFormat:@"My retain count is 1"]
[s autorelease]; // yes its retained
[s release]; // bad idea

In this example ownership is transferred to the autorelease pool. s can
not die earlier than the enclosing autorelease pool.

"Ownership" is not "transferred" to the autorelease pool, the program simply
crashes later at the end of the event loop when the autorelease pool is released
and it sends its release on the "autoreleased" object which no longer has a retain
count and has already been deallocated.

Always balance your alloc/copy/retain with releases if you have no further need
for the object or a final autorelease to bring the count to zero if you return the
object. This way (in general) it's "minimum lifetime" will be to the end of the
current event loop - when Cocoa releases and recreates the default autorelease pool.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.