(reprint) OC Study---The reference counting problem of array objects and the concept of an auto-free pool

Source: Internet
Author: User

in a previous article we introduced the use of two keywords in OC @property and the use of @synthesize, today, let's take a look at how the array object in OC deals with the reference counting of object elements, and introduces the concept of the automatic release pool

How array objects handle reference counting for object elements

1 //  2 //main.m3 //26_nsarraymemerymanager4 //  5 //Created by Jiangwei on 14-10-12. 6 //Copyright (c) 2014 Jiangwei.  All rights reserved. 7 //  8   9 #import<Foundation/Foundation.h>Ten    One #import "Dog.h"   A    - intMainintargcConstCharchar *argv[]) {   -        theDog *dog1 =[[Dog alloc] init];  -Dog *dog2 =[[Dog alloc] init];  -        -Nsmutablearray *array =[[Nsmutablearray alloc] init];  +        -     //the array is retain for each element +[Array addobject:dog1];//dog1 Count =2 A[Array addobject:dog2];//dog2 Count =2 at        - [Dog1 release];  - [DOG2 release];  -        -     //when the array is destroyed, all the elements are release -[Array release];//Array Destruction in        -     //when the array removes all the elements, it will say all the elements release to [Array removeallobjects];  +        -     return 0;  the}

we define the dog class and then define the Nsmutablearray array to hold two dog objects, and the Retain method is automatically called when the object is placed in the array, and when the array object itself is destroyed, the release method of all the elements is called. The release method of the element is called when all elements in the array are moved

Second, the concept of automatic release pool

1 //  2 //main.m3 //27_autoreleasepool4 //  5 //Created by Jiangwei on 14-10-13. 6 //Copyright (c) 2014 Jiangwei.  All rights reserved. 7 //  8   9 #import<Foundation/Foundation.h>Ten    One #import "Dog.h"   A    - intMainintargcConstCharchar *argv[]) {   -        the     /*  - //Create an auto-release pool - //There is a scope problem, the outside of what is defined in {} is inaccessible, this is different from NSAutoreleasePool - @autoreleasepool {//equivalent to [[NSAutoreleasePool alloc] init] +          - Dog *DOG2 = [[Dog alloc] init]; + [dog2 retain]; A           at }//equivalent to [pool release] -       - //Create an auto-release pool - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; -       - Dog *dog1 = [[Dog alloc] init];//count: 1 in       - //Add the Dog1 object to the auto-release pool, but not the previous release method to //Join to the auto-release pool, it's not that we don't need to manage references, but the auto-release pool automatically calls the release + //When the auto-release pool is destroyed, releasing the pool calls the release once for each object in the pool - [Dog1 autorelease]; the NSLog (@ "Dog1 count:%ld", dog1.retaincount); *       $ //Destroy auto-release poolPanax Notoginseng //This will call Dog1 's release method and the Dog1 object will be destroyed. - [Pool release]; the     */   +        A     //Auto-free pool nesting theNSAutoreleasePool *pool1 =[[NSAutoreleasePool alloc] init];  +        -     //Add our Code $     //Dog1 put it in the pool1. $Dog *dog1 =[[Dog alloc] init];  - [Dog1 autorelease];  -        the     //Auto-free pool nesting -NSAutoreleasePool *pool2 =[[NSAutoreleasePool alloc] init]; Wuyi        the     //Dog2 put it in the pool2. -Dog *dog2 =[[Dog alloc] init];  Wu [Dog2 autorelease];  -        About     //Pool2 destroyed the $ [Pool2 autorelease];  -        -     //Pool1 destroyed the - [Pool1 release];  A        +        the     //The following code is a problem -     //[Person Setdog:[[dog alloc] init];  $     //the correct wording the     //Dog *dogs = [[[Dog alloc] init] autorelease];  the        the        the         -     return 0;  in}

In our previous article, when we defined an object, we created an auto-release pool, and then we wrote our code in the release pool, which is a system-provided reference counting problem that helps us to manage objects. But sometimes the code must be in {...} This will create a scope problem, that is, in the {...} The variables defined in the {...} cannot be used outside. So there's another way in OC: NSAutoreleasePool this class.

This automatic release pool enables nesting

1 nsautoreleasepool *pool1 = [[NSAutoreleasePool alloc] init]; 2 3 // Write code ... 4 5  6 7 // Pool1 destroyed the 8 9 [pool1 release];

The above code is equivalent to creating an auto-release pool pool1, but in this middle of the code, if you want to join this pool, you must call the Autorelease method:

1 // Dog1 put it in  the pool1. 2 Dog *dog1 = [[Dog alloc] init];   3 [Dog1 autorelease];  

Moreover, this definition of a pool can also be nested use, directly look at the above example code, so that the automatic release of the pool we can control. There are a lot more places to operate than the system-provided auto-release pool.

Here's a direct comparison:

1 nsautoreleasepool *pool1 = [[NSAutoreleasePool alloc] init]; 2 {3         [Pool1 release]; // This line of code is equivalent to the system automatically releasing the pool 4 }

Well, that's a good idea.

Summarize

This article mainly introduces the reference problems that need to be handled when the array objects manipulate element objects in OC, and we can customize an auto-release pool, which is more convenient and operable than the automatic release pool provided by the system.

(reprint) OC Study---The reference counting problem of array objects and the concept of an auto-free 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.