Learn how to use NSAutoreleasePool in Objective-c

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.