OC Learning---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 @synthesize:

http://blog.csdn.net/jiangwei0910410003/article/details/41925967

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

  main.m//  26_nsarraymemerymanager////  Created by Jiangwei on 14-10-12.//  Copyright (c) 2014 Jiangwei. All rights reserved.//#import <Foundation/Foundation.h> #import "Dog.h" int main (int argc, const char * argv[]) {
   
    dog *dog1 = [[Dog alloc] init];    Dog *DOG2 = [[Dog alloc] init];        Nsmutablearray *array = [[Nsmutablearray alloc] init];        The array retain [array addobject:dog1] for each element    ,//dog1 count =2    [array addobject:dog2];//dog2 count =2        [DOG1 release] ;    [DOG2 release];        When the array is destroyed, all the elements will be release    [array release];//array destroyed        //When the array removes all elements, it will say all the elements release    [array Removeallobjects];        return 0;}
   
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

main.m//27_autoreleasepool////Created by Jiangwei on 14-10-13.//Copyright (c) 2014 Jiangwei.        All rights reserved.//#import <Foundation/Foundation.h> #import "Dog.h" int main (int argc, const char * argv[]) {  /*//Create an auto-free pool//scoped problem that is not accessible outside of what is defined in {}, which differs from NSAutoreleasePool @autoreleasepool {///is equivalent to [[NSAutoreleasePool        ALLOC] init] Dog *dog2 = [[Dog alloc] init];            [Dog2 retain];        }//equivalent to [pool release]//Create an auto-release pool NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; Dog *DOG1 = [[Dog alloc] init];//count: 1//Add the Dog1 object to the auto-release pool, but not before the release method//join to the auto-release pool, does not mean that we do not need to manage the reference, but the automatic release of the pool automatically    Call Release once//when the auto-release pool is destroyed, releasing the pool will call release [Dog1 Autorelease] For each object in the pool.        NSLog (@ "Dog1 count:%ld", Dog1.retaincount);    Destroy auto-Release pool//This time call Dog1 's release method, the Dog1 object is destroyed [pool release];        */////////auto-release pool nesting nsautoreleasepool *pool1 = [[NSAutoreleasePool alloc] init]; Add our code//dog1 put it in pool1. Dog *dOg1 = [[Dog alloc] init];        [Dog1 Autorelease];        Auto-free Pool nesting nsautoreleasepool *pool2 = [[NSAutoreleasePool alloc] init];    Dog2 put in pool2 dog *dog2 = [[Dog alloc] init];        [Dog2 Autorelease];        Pool2 destroyed the [pool2 autorelease];            Pool1 destroyed [pool1 release];    The following code is the problematic//[person Setdog:[[dog alloc] init];                 The correct wording//dog *dogs = [[[Dog alloc] init] autorelease]; return 0;}
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

NSAutoreleasePool *pool1 = [[nsautoreleasepool alloc] init];

Write code ...

Pool1 destroyed the

[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:

Dog1 put in pool1 dog *dog1 = [[Dog alloc] init]; [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:

NSAutoreleasePool *pool1 = [[nsautoreleasepool alloc] init];

This line of code is equivalent to the system automatically releasing the pool {

[pool1 release];

This line of code is equivalent to the system automatically releasing the pool}

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.





OC Learning---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.