Rules:
- When you create an object using the new, alloc, or Copy method, the value of the reserved counter for the object is 1. When the object is no longer being used, you should send a release or autorelease message to the object. In this way, the object will be destroyed at the end of its useful life.
- When you get an object by other means, assume that the value of the object's retention counter is 1, and that it has been set to auto-release, you don't need to do anything to ensure that the object is cleaned up. If you intend to have the object for a period of time, you need to keep it and make sure that it is released when the operation is complete.
- If you keep an object, you need to (eventually) release or release the object automatically. The Retain method must be kept equal to the number of times the release method is used.
The auto-free pool is implemented as a stack: When you create a new auto-free pool, it is added to the top of the stack. The object that receives the Autorelease message is placed in the topmost auto-free pool. If you put an object in an auto-free pool and then create a new auto-free pool, and then destroy the newly created auto-free pool, this auto-free pool object will be thrown because the auto-free pool that holds the object still exists.
Note:
Stack memory allocation principle: from high to bottom allocation, from low to high access.
On the code:
Project Documents-Download link
MAIN.M file:
////MAIN.M////Created by on 15/4/13.//Copyright (c) 2015. All rights reserved.//#import <Foundation/Foundation.h> #import "Person.h" #import "Dog.h" intMainintargcConst Char* argv[]) {@autoreleasepool {//person *aperson = [[Person alloc] init];//NSLog (@ "reference count:%ld", aperson.retaincount);// //[Aperson release];//Call Dealloc now// ////[Aperson autorelease];//To '} ' when Dealloc is called// //NSLog (@ "reference count:%ld", aperson.retaincount);//reference count cannot be printed 0//Person *aperson = [[Person alloc] init];//1Dog *adog = [[Dog alloc] init];//1Aperson. Pet= Adog;//2 //When executing the Copy method, the method inside the methods is executed in the Nscopying protocol, Copywithzone:Dog *newdog = [Adog copy];NSLog( @"%@", Newdog); [Adog release];//1[Aperson release];//0//Note:/* 1, the increase and decrease of the reference count is equal, when the reference count is reduced to 0, the memory space should not be used again, 2, generally used alloc, retain or copy, new, mutablecopy to let the memory reference count increased, You need to use release or autorelease to reduce the reference count for memory. Within a piece of code, the number of increments and decreases is equal; 3. If you have ownership of an object, you need to release ownership of the object when it is no longer in use. If an object is not owned by us, do not release ownership of the operation. */}return 0;}
Person.h file:
////Person.h////Created by on 15/4/13.//Copyright (c) 2015. All rights reserved.//#import <Foundation/Foundation.h> #import "Dog.h" @interface Person: nsobject @property(nonatomic, retain)NSString*name;@property(nonatomic,Assign)NsintegerAge;@property(nonatomic, retain)NSString*gender;@property(nonatomic, copy) Dog *pet;@end
PERSON.M file:
//// Person.m//// Created by on 15/4/13.// Copyright (c) 2015年 . All rights reserved.//#import "Person.h"@implementation Person- (void)dealloc { NSLog( @"%s", __FUNCTION__ ) ;// NSLog( @"%@被销毁啦", self ) ; [_pet release] ; [super dealloc] ;}@end
Dog.h file:
// Dog.h // //Created by on 15/4/13. //Copyright (c) 2015. All rights reserved. // #import <FOUNDATION/FOUNDATION.H> Span class= "Hljs-comment" >//if the object of a class wants to be copied, it is necessary to follow the Nscopying protocol and implement the relevant method of the protocol @interface dog : nsobject <nscopying ; @property (nonatomic , retain) nsstring *name; @end //dog:nsobject
DOG.M file:
////DOG.M////Created by on 15/4/13.//Copyright (c) 2015. All rights reserved.//#import "Dog.h" @implementation Dog - (void) Dealloc {NSLog( @'%s ', __function__); [SuperDealloc];}#pragma mark-nscopying-- (ID) Copywithzone: (Nszone *) Zone {//Create replica objects based on memory information zoneDog *dogcopy = [[Dog allocwithzone:zone] init]; Dogcopy. Name= Self. Name;NSLog( @"Address of the Replica object:%p", dogcopy);returnDogcopy;}@end
OBJECTIVE-C----Memory Management