views:

880

answers:

3

The doc says:

In a garbage-collected environment, sending a drain message to a pool triggers garbage collection if necessary; release, however, is a no-op. In a reference-counted environment, drain has the same effect as release. Typically, therefore, you should use drain instead of release.

If I get that right, they say that I should always use -drain, doesn't matter if there's Garbage Collection around or Reference Counting. On iPhone is no GC, so anyways I send -drain?

Unfortunately, the doc only talked about Cocoa, not Cocoa Touch or iPhone. So I am not sure if the same applies there.

+5  A: 

It doesn't really matter, they both have the same effect. Apple recommends using drain in case you ever move to a garbage collected environment (if a future version of iPhone SDK supports it, or if you copy it to a Mac OS X project), so that's what I'd go with.

Marc Charbonneau
+1  A: 

Just send -drain; if at some later date the iPhone has GC you'll be able to compile it for that with no modification. It's a good habit to get into.

Patrick McCafferty
Thanks. I was wondering a little bit because Apple sent a -release to the ARP in the main() function (main.m).
Thanks
A: 

In normal development there's no need to call drain on the autorelease pool - it will do that anyway when your method returns.

The exception would be if you are in a loop creating many objects that are autoreleased, and you have the opportunity to free some of them earlier.

If you are calling drain as a replacement for release, it will not free objects that have not been autoreleased.

Kendall Helmstetter Gelner