What's the difference between sending -release or -drain to an Autorelease Pool?
Asked Answered
D

3

19

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.

Dyke answered 28/4, 2009 at 11:28 Comment(0)
M
25

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.

Moonmoonbeam answered 28/4, 2009 at 18:25 Comment(3)
Thanks. So on iPhone OS it's better to use -release rather than -drain?Dyke
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+.Moonmoonbeam
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.Moonmoonbeam
C
10

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

Clo answered 28/4, 2009 at 11:33 Comment(5)
-retain and -drain methods does not change retainCount of NSAutoreleasePool object They only send release message to all objects in pool I don't know, how and when NSAutoreleasePool objects are destroing (((Clo
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).Yeisk
Marc Charbonneau But 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 = 1Clo
That shouldn't be. Actually, the ARP should get destroyed after -release, right?Dyke
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.Moonmoonbeam
U
10

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.

Ulster answered 28/4, 2009 at 11:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.