Autorelease detailed
/ * Basic usage of 1.autorelease 1> objects are placed in an auto-free pool 2> when the auto-free pool is destroyed, all objects inside the pool are released once 3> returns the object itself 4> After calling the Autorelease method, the object's counter does not change the benefits of 2.autorelease 1> no longer care about the time the object is released 2> no longer care about when to call release 3.autorelease use note 1> Objects that occupy large memory do not use the Autorelease 2> objects that consume less memory use autorelease, and do not have much effect 4. Error notation 1> alloc, and call Release @ Autoreleasepool {//1 person *p = [[Person alloc] init] autorelease]; 0 [P release]; } 2> consecutive calls to Autorelease @autoreleasepool {person *p = [[[Person alloc] init] autorelease] autorelease];} 5. Auto-Release pool 1> an unlimited number of pools are created during the iOS program run. These pools are present in a stack structure (advanced) 2> when an object calls the Autorelease method, the object is placed at the top of the heap to release Pool 6. How the auto-free pool is created 1> IOS 5.0 ago NSAutoreleasePool *pool = [[ NSAutoreleasePool alloc] init]; [Pool release]; [Pool drain]; 2> IOS 5.0 starts @autoreleasepool {} * /#Import<foundation/foundation.h>#Import "Person.h"intMain () {NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; Person *pp = [[[Person alloc] init] autorelease]; [Pool release];//[pool drain]; @autoreleasepool{//1Person *p = [[[Person alloc] init] autorelease] autorelease];//0 //[P release];}return 0;}voidTest () {@autoreleasepool{//{Start representing the release pool created The //Autorelease method returns the object itself //After the Autorelease method is called, the object's counter does not change //Autorelease the object into an auto-release pool //When the auto-release pool is destroyed, a release operation is made for all objects inside the poolPerson *p = [[[Person alloc] init] autorelease]; P.age =Ten;@autoreleasepool{//1Person *P2 = [[[Person alloc] init] autorelease]; P2.age =Ten; } Person *p3 = [[[Person alloc] init] autorelease]; }//} end represents destruction of the release pool}
@interface Person : NSObject@property (nonatomicassignint age;@end
#import "Person.h"@implementation Person- (void)dealloc{ NSLog(@"Person---dealloc"); [super dealloc];}@end
Application of autoreleased
#import "Person.h" #import "GoodPerson.h" / * 1. The system comes with a method that does not contain alloc, new, copy, indicating that the returned objects are autorelease 2. In development, you will often provide some class methods to quickly create an object that has been autorelease 1> Do not use the class name directly when creating an object, usually with self + (ID) person {return [[[Self alloc] init] autorelease];} */intMain () {@autoreleasepool {person *p = [Person personwithage: -]; Goodperson *P2 = [Goodperson personwithage:Ten]; P2. Money= -; }return 0;}voidTest () {Person *p = [[Person alloc] init]; P. Age= $; [P release];//Bag //Student com.mj.test //Student com.mj.test2 //Mjstudent //Sbstudent //Next Step@autoreleasepool {//person *p2 = [person person]; // //P2.age = +;Person *P2 = [Person personwithage: -];//person *P2 = [[[Person alloc] init] autorelease]; // //P2.age = +; NSString*STR = @"123123";NSString*STR2 = [NSStringstringwithformat:@"Age is%d",Ten];NSNumber*num = [[NSNumberAlloc] Initwithint:Ten]; [Num release];NSNumber*NUM2 = [NSNumberNumberwithint: -]; }}
@interface person : nsobject @property (nonatomic , assign ) int age;+ (id ) person;+ (id ) Personwithage: (int ) age; @end
@implementati On person + (id ) person{return [[[[self alloc] init] autorelease];} + (id ) Personwithage: (int ) age{person *p = [ Span class= "Hljs-keyword" >self person]; P.age = age; return p;} -(void ) dealloc{nslog (@"%d-year-old man was destroyed ", _age); [super dealloc];} @end
#import "Person.h"@interface GoodPerson : Person@property (nonatomicassignint money;@end
@implementation GoodPerson@end
Objective-c-Autorelease Detailed