Wednesday, March 23, 2011

release Vs drain in autorelease pool

An autorelease pool stores objects that are sent a release or autorelease message and are deallocated when the autorelease pool itself is drained.

If a question like "what to use?  'drain' or 'release' ? comes in your mind, below is the simple resolve for your question.

release:
In a reference-counted environment, since an autorelease pool cannot be retained , this method causes the receiver to be deallocated. When an autorelease pool is deallocated, it sends a 'release' message to all its autoreleased objects. Also, In a garbage-collected environment, this method is a no no.

- (void)release

drain:
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.

- (void)drain

Conclusion:
From the above brief discussion it is clear that, we should always use 'drain' over 'release' for an autorelease pool(be the Cocoa or Cocoa touch).



No comments:

Post a Comment