views:

1414

answers:

3

In many Books and on many Sites I see -drain. Well, for an Autorelease Pool that sounds cool. But does it do anything other than an release? I would guess -drain just makes the Pool to -release all it's objects, without releasing the Pool itself. Just a guess.

+6  A: 

If your system has a garbage Collection, then -drain send message (objc_collect_if_needed) for GC

If you haven't GC, then drain = release

oxigen
so a -drain would also release the pool itself, right?
Thanks
-retain and -drain methods does not change retainCount of NSAutoreleasePool objectThey only send release message to all objects in poolI don't know, how and when NSAutoreleasePool objects are destroing (((
oxigen
No, drain does release the pool. There's no magic when it comes to autorelease pools and retain counts, they follow the same rules as any other object except they can't be retained or autoreleased (which wouldn't make much sense anyway).
Marc Charbonneau
Marc CharbonneauBut why I can write:NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];[pool release];[pool release];[pool release];[pool release];.....[pool release];And it's work. And even after this, pool retainCount = 1
oxigen
That shouldn't be. Actually, the ARP should get destroyed after -release, right?
Thanks
oxigen, in your example there's no allocation happening after your releases, so you are likely just interacting with the intact-but-now-garbage memory of a destroyed object. Just because a call to a destroyed object happens to work doesn't mean that call is valid. As for retainCount returning 1, perhaps retainCount just always returns 1 for NSAutoreleasePool since it has unusual retain semantics.Even if there were some implementation detail making it last slightly longer, since the documentation explicitly says it's deallocated when drained or released you should assume that it is.
smorgan
+6  A: 

Oxigen is right, see the documentation for method drain of NSAutoreleasePool:

In a reference-counted environment, releases and pops the receiver; in a garbage-collected environment, triggers garbage collection if the memory allocated since the last collection is greater than the current threshold.

zoul
+2  A: 

Note that the comments on oxigen's answer saying that -drain does not release the NSAutoreleasePool are not correct. The documentation for NSAutoreleasePool clearly says that -drain releases (and thus destroys) the NSAutoreleasePool.

-drain is a replacement for using -release for NSAutoreleasePool objects, the only difference being that provides a hint to the garbage collection system.

smorgan
Thanks. So on iPhone OS it's better to use -release rather than -drain?
Thanks
In a non-GC environment they are the same. Except in the rare case where you wouldn't want to provide a hint to the GC system for some reason if the code were ever used in GC mode, there's no reason to use release instead of drain in code targeting 10.4+.
smorgan
Apple uses -release for the ARP in the main() function...
Thanks
Apple doesn't know in advance if someone will be making their program target pre-10.4, and there's no value in providing a hint to the GC system just before exiting anyway, making release a better choice in that specific case.
smorgan