"Reading notes" ios-automatic release Pool

Source: Internet
Author: User

One, the NSObject class provides a autorelease method:

-(ID) autorelease;

This method pre-sets a release message to be sent at a later time, and the return value is the object that receives the message. The retain message uses the same technique to make nested calls easier. When you send an autorelease message to an object, you are actually adding the object to NSAutoreleasePool. When the auto-free pool is destroyed, a release message is sent to all objects in the pool.

Two

NSAutoreleasePool *pool;

Pool=[[nsautoreleasepool Alloc]init];

...

[Pool release];

When you create an auto-free pool, the pool automatically becomes the active pool. When the pool is released, its retention counter value is 0, and then the pool is destroyed. During the destruction process, the pool releases all of its contained objects.

When you use AppKit, cocoa automatically creates and destroys auto-free pools for you on a regular basis. This is usually done after the program finishes processing the current event, such as a mouse click or key. You can use as many auto-releasing objects as you like, and when you no longer use them, the auto-free pool will automatically clean up those objects for you.

Third, you may have encountered another way to destroy objects in the auto-release pool in Xcode's auto-generated code: the-drain method. This method simply empties the pool automatically and does not destroy it. The-drain method is only available for Mac OS X 10.4 and later. In the code we write ourselves (not generated by Xcode), we use the-release method because it works for all versions of Mac OS.

Reference: "Object-c Basic Course"

"Reading notes" ios-automatic release Pool

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.