Measure the test taker's knowledge about how to use the NSAID oreleasepool in Objective-C.

Source: Internet
Author: User

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!

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.