The purpose of this article is to understand how NSAutoreleasePool is used in objective-c , andobjective-c 's foundation library is actually a run-level object system. Unlike general object languages, such as C++,java, objects, like COM or CORBA, are not necessarily created in the address space of a user application, but may be created in another address space or even created on another machine!
Therefore,objective-c objects (all inherited from NSObject), is the use of reference counting methods to manage the survival of objects, it is well known that when the reference count is 0 o'clock, the object is destroyed. The operation is very simple, and when the object is created, the reference count is set to 1. You can send an retain message to an object, allowing the object to add 1 to its reference count.
When the object accepts the release message, the object will reduce its reference count by 1, and when the reference count is 0, the object will call its own dealloc processing, all need the programmer to pro-force, a little sloppy.
But, what is NSAutoreleasePool? It is actually an object reference count automatic processor.
First of all, NSAutoreleasePool can have multiple at the same time, its organization is a stack, there is always a stack top pool, that is, the current pool, each create a pool, the stack to press A, change the current pool for the new pool, and then, Each time you send a drain message to the pool, it pops up the pool at the top of the stack and changes the current pool to the next pool in the stack.
The next thing to note is that the object is not automatically added to the current pool, but instead it needs to send a autorelease message to the object so that the object is added to the current pool management.
When the current pool receives a drain message, it simply sends a release message to all objects it manages.
Here, you will find that this so-called pool has a deceptive feature, that is, if the managed object in the pool receives the release message sent by the pool, its reference count is greater than 0, then the pool disappears, but that object still exists!
Learn how to use NSAutoreleasePool in Objective-c