The purpose of this article is to understandObjective-CMediumNSAID utoreleasepoolUsage,Objective-CThe Foundation library of is actually a running-level Object System, which is different from common object languages such as C ++ and Java, and is the same as that of COM or Corba, not necessarily created in the address space of your application. It may be created in another address space or even on another machine!
So,Objective-CAll objects are inherited from NSObject), that is, the reference count method is used to manage the survival of objects. As we all know, when the reference count is 0, the objects are destroyed. The operation is very simple. when an object is created, the reference count is set to 1. You can send a retain message to an object to add 1 to its reference count.
When an object receives a release message, the object will subtract 1 from its reference count. When the reference count reaches 0, the object will call its own dealloc for processing, everything needs to be done by programmers.
But what is the NSAID utoreleasepool? It is actually an automatic processor of object reference count.
First, there can be multiple NSAID utoreleasepools at the same time, and its organization is a stack. There is always a stack top pool, that is, the current pool. Each time a pool is created, one pool is pressed into the stack, change the current pool to the new pool. Then, each time a drain message is sent to the pool, the pool at the top of the stack pops up, and change the current pool to the next pool in the stack.
Next, you should note that the object is not automatically added to the current pool, but needs to send the autorelease message to the object. In this way, the object is added to the management of the current pool.
When the current pool receives a drain message, it simply sends a release message to all objects it manages.
Here, we will find that this so-called pool has a pitfall feature, that is, if the managed object in the pool receives the release message sent by the pool, if its reference count is greater than 0, the pool disappears, but the object still exists!
Summary: UnderstandingObjective-CMediumNSAID utoreleasepoolI hope this article will help you!