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 Waterfoxjie on 15-03-29. //Copyright (c) 2015 Waterfoxjie. All rights reserved. // #import<Foundation/Foundation.h>#import "Dog.h" intMainintargcConstCharchar *argv[]) {Dog*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[Array addobject:dog2];//dog2 Count =2[Dog1 release]; [DOG2 release]; //when the array is destroyed, all the elements are release[Array release];//Array Destruction//when the array removes all the 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, it is called
The release method of all elements that, when all elements in the array are moved, invoke the release method of the element
Second, the concept of automatic release pool
// //main.m//27_autoreleasepool// //Created by Waterfoxjie on 15-03-29. //Copyright (c) 2015 Waterfoxjie. All rights reserved. // #import<Foundation/Foundation.h>#import "Dog.h" intMainintargcConstCharchar *argv[]) { /*Create an auto-free pool//scoped problem that is not accessible outside of what is defined in {}, which differs from NSAutoreleasePool
@autoreleasepool {//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 the previous release method//join to the auto-release pool After, does not mean that we do not need to manage the reference, but the automatic release of the pool will automatically call the release//when the automatic release of the pool destroyed, the release of the pool to each object in the pool to call the release
[Dog1 Autorelease];
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-free pool nestingNSAutoreleasePool *pool1 =[[NSAutoreleasePool alloc] init]; //Add our Code//Dog1 put it in the pool1.Dog *dog1 =[[Dog alloc] init]; [Dog1 Autorelease]; //Auto-free pool nestingNSAutoreleasePool *pool2 =[[NSAutoreleasePool alloc] init]; //Dog2 put it in the pool2.Dog *dog2 =[[Dog alloc] init]; [Dog2 Autorelease]; //Pool2 destroyed the[Pool2 autorelease]; //Pool1 destroyed the[Pool1 release]; //The following code is a problem//[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 it in the pool1.
Dog *DOG1 = [[Dog alloc] init];
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]; // }
Well, that's a good idea.
Dark Horse Programmer--objective-c-The concept of automatic release pool and its use