iOS basic memory management: Autorelease and Autoreleasepool

Source: Internet
Author: User

1.autorelease Basic Usage

    • object is added to the auto-release pool when the Autorelease method is executed
    • Automatically frees all objects in the pool for release when the auto-free pool is destroyed
    • The Autorelease object does not change its own reference counter after executing the method, and it returns the object itself

Advantages of 2.autorelease

    • Autorelease actually just delayed the call to release, and for each autorelease system just put the object into the current Autorelease pool, when the pool is released, All objects in the pool are called release
    • Because the objects in it are destroyed only when the auto-free pool is destroyed, so you don't have to worry about when the object is destroyed, you don't have to worry about when to call release.

3.autorelease Use note

    • Do not use when you are using a large memory object, fearing that the object will be released too late
    • An object that uses less memory for operations can use

4.atureleasepool Auto-Release pool

Auto-free pool is stored in the in-memory stack followed by the "advanced out" principle

  1. #import <Foundation/Foundation.h>
  2. #import "Person.h"
  3. int main (int argc, const char * argv[])
  4. {
  5. Auto Free Pool 1
  6. @autoreleasepool {
  7. Release of object to auto-release pool to manage no more writing [person release]
  8. Person *person = [[[Person alloc] init] autorelease];
  9. Then create an auto-release Pool 2
  10. @autoreleasepool {
  11. Person *person2 = [[[Person alloc] init] autorelease];
  12. }
  13. Person *person3 = [[[Person alloc] init] autorelease];
  14. }
  15. return 0;
  16. }

As you can see from the code above, the first thing that executes the code is that the Person2 object is destroyed first, then the outer object, the person and the PERONS3 memory, behave as follows:

5. Using common errors

    1. Destroy the automatic release pool when you want to perform a release operation on the person, the field pointer error is reported
    2. @autoreleasepool {
    3. Person *person = [[[Person alloc] init] autorelease];
    4. [Person release];
    5. }
    1. Object executes two times autorelease means that the object executes two times when the auto-free pool is destroyed the release operation will report the wild pointer error
    2. @autoreleasepool {
    3. Person *person = [[[Person alloc] init] autorelease] autorelease];
    4. }

iOS basic memory management: Autorelease and Autoreleasepool

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.