@autoreleasepool semantics

I read the ARC docs at llvm: http://clang.llvm.org/docs/AutomaticReferenceCounting.html#autoreleasepool

..in particular about @autoreleasepool.

In a lot of the current implementation using NSAutoreleasePool, I see cases where the pool periodically merges during the iteration loop - how will we do the same with the @autorelease pool, or is this all done for us somehow under the hood?

Secondly, the documents indicate that if an exception is thrown, the pool does not merge .... exceptions are thrown by default, but if they really happen, you may need to recover without a load leak. Documents do not indicate when these objects will be released.

Has anyone received information about these points?

+7
source share
2 answers

In a lot of the current implementation using NSAutoreleasePool, I see cases where the pool periodically merges during the iteration loop - how will we do the same with the @autorelease pool, or is this all done for us somehow under the hood?

In the same way, that is, by cascading pools of abstracts. For example:

@autoreleasepool { … for (int i = 0; i < MAX; i++) { @autoreleasepool { … } } … } 

Secondly, the documents indicate that if an exception is thrown, the pool does not merge .... exceptions are thrown by default, but if they really happen, you may need to recover without a load leak. Documents do not indicate when these objects will be released.

In most cases, the program will not be able to gracefully recover due to the specifics of exceptions in Cocoa, so Id says that leaking objects is a lesser problem. If, due to an exception, the @autoreleasepool block @autoreleasepool , the corresponding auto-implemented objects will be released only when one of its autoplay pools is released. But, of course, you can place @try/@catch/@finally blocks inside the @autoreleasepool block to prevent this from happening.

+9
source

how will we do the same with the @autorelease pool

Like this:

 for (int i = 0; i < 10000; i++) { @autoreleasepool { // Do your work here ... } } 

Secondly, the documents indicate that if an exception is thrown, the pool does not merge .... exceptions are thrown by default, but if they really happen, you may need to recover without a load leak.

AFAIK is not possible with ARC. ARC is no exception at all. If an exception occurs, there is the possibility of irreparable memory leaks. Code using ARC should not rely on exceptions for error reporting. The process is expected to crash when an exception is thrown.

+2
source

All Articles