Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does an @autoreleasepool {} drain when returning / exiting early?

Consider this example:

- (void)doSomething {
   @autoreleasepool {
      if (someCondition) {

         /* ... allocate some autoreleased objects here ... */

         return;
      }
   }
}

Previously, with manual NSAutoreleasePools, if we returned early, we needed to call [pool drain], otherwise the pool would not be drained. With the new @autoreleasepool {}

like image 457
Martijn Thé Avatar asked Feb 09 '12 10:02

Martijn Thé


1 Answers

The answer is YES:

When the block is exited normally, whether by fallthrough or directed control flow (such as return or break), the autorelease pool is restored to the saved state, releasing all the objects in it. When the block is exited with an exception, the pool is not drained.

Source: http://clang.llvm.org/docs/AutomaticReferenceCounting.html#autoreleasepool

like image 191
Martijn Thé Avatar answered Nov 06 '22 12:11

Martijn Thé