Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 820
Thanks Avatar asked Apr 28 '09 11:04

Thanks


People also ask

What is Autorelease pool?

An autorelease pool stores objects that are sent a release message when the pool itself is drained. Important. If you use Automatic Reference Counting (ARC), you cannot use autorelease pools directly. Instead, you use @autoreleasepool blocks.

Why retainCount should never be used in shipping code?

You should never use -retainCount , because it never tells you anything useful. The implementation of the Foundation and AppKit/UIKit frameworks is opaque; you don't know what's being retained, why it's being retained, who's retaining it, when it was retained, and so on.


3 Answers

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.

like image 113
smorgan Avatar answered Oct 15 '22 01:10

smorgan


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

like image 39
oxigen Avatar answered Oct 15 '22 01:10

oxigen


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.

like image 10
zoul Avatar answered Oct 15 '22 01:10

zoul