What is autoreleasepool?

Possible duplicate:
Why use the auto-update pool?

The Objective-C start page opens with the @autoreleasepool{...} default instruction in the main function declaration. But what does this expression really do? The new Objective-C automatically frees objects and deleting a line does not change anything in the program. Is this command really necessary?

+45
object objective-c release nsautoreleasepool autorelease
Feb 03 '13 at 20:39
source share
1 answer

The @autoreleasepool does the same job as before, instead of using the NSAutoreleasePool class. NSAutoreleasePoolโ€™s way of working was a little strange, since creating it had an effect throughout the application; @autoreleasepool creates a scope with a scope and gives a clearer picture of what is inside the pool and when it merges (when it goes out of scope). It is also more efficient according to Apple.

The concept of the autoresist pool is simple, whenever an instance of an object is marked as auto-implemented (for example, NSString* str = [[[NSString alloc] initWithString:@"hello"] autorelease]; ), it will have a hold value of +1 at this point in time , but at the end of the loop, the pool merges, and any object marked with autorelease then has a save account reduced. This is a way to keep the object around while you cook everything that will keep it for yourself.

With ARC, while the autorelease keyword is not used by the developer, the base system that manages ARC inserts is for you. ( Remember: all ARC inserts retain , release and autorelease calls you at the appropriate times.) Because of this, the existing AutoreleasePool concept must remain.

If you delete the auto resource pool, your objects will start to leak

In a link-counting environment, Cocoa expects the auto-ad pool to always be available. If the pool is unavailable, auto-implemented objects are not freed and you skip memory. In this situation, your program usually records the appropriate warning messages.

+70
Feb 03 '13 at
source share



All Articles