2. Use
1.1 Sending an Autorelease message to an object will place the object in an auto-free pool 1.2 when the auto-free pool is destroyed, all objects inside the pool are given a release operation 1.3 Returns the object itself 1.4 after calling the Autorelease method, The counter of the object does not change 2. Benefit 2.1 No longer care about the time the object is freed, it is helpful to avoid the advent of bad memory access 2.2 no longer care about when to call Release 3. Use note 3.1 Memory-intensive objects do not use autorelease3.1 to consume small objects using autorelease, not much impact 4.autoreleasepoolautoreleasepool is stored in a stack structure, advanced , only the pool at the top of the stack is active, and the object can be mounted. And because it is a stack structure, it is destroyed by first destroying the pool at the top of the stack and finally the bottom of the stack.
Example code:
@autoreleasepool { //pool at the bottom of the stack
@autoreleasepool { //pool in the middle
@autoreleasepool { //pool at top of stack ..... You can also nest nested
People *people = [[[[People alloc] init] autorelease];
}
Goodpeople *goodpeople = [[[[Goodpeople alloc] init] autorelease];
}
Badpeople *badpeople = [[[[Badpeople alloc] init] autorelease];
}
PEOPLE.M file-(void) dealloc{NSLog (@ "person released"); [Super Dealloc];} GOODPEOPLE.M file-(void) dealloc{NSLog (@ "Good Man released"); [Super Dealloc];} BADPEOPLE.M file-(void) dealloc{NSLog (@ "bad guys released"); [Super Dealloc];} Console print results: The man freed The Good Man and released the bad guy.
5. How the auto-release pool is created5.1-Tail5.2 Curly-Bracescommon errors in 6.autorelease6.1Person *p = [[[Person alloc] init] autorelease];
[P release]; //This line of code is executed, p will not exist, and so on when P is destroyed, will send P release message, then reported bad access error
6.2 Person *p = [[[Person alloc] init] autorelease] autorelease]; //This line of code, indicating that the pool was destroyed, sent 2 release messages to the object P, sending a 2nd release message is a bad access error
7. A class method for quickly creating objects, commonly used in conjunction with Autorelease, in MRC
+ (instancetype) book {
return [[[Self alloc] init] autorelease];
}
The benefits of using Autorelease in class methods for creating objects:
1> save code, outside if you want to create a book, the direct tune of the [book Book] on the line, do not need to write so long, [[[Book Alloc] init] autorelease]
2> return objects to others when used, others do not need to tube memory. For example Book *b = [book book]; no one else needs to release it, because the book method has been autorelease inside.
3> from the point of view of memory management principles, because the book method, not Alloc, nor retain, so do not need release
8. Many system comes with the method, return is also the Autorelease object
/** the string created is to release the NSString *str= [NSString alloc] init]; [Str release];*/system comes with method [NSString stringwithformat:@ "to create a string without release"]; The returned object is Autorelease, so no release required generally, objects created except Alloc, new, or copy are declared autorelease such as the following objects are already autorelease, No need to release again
NSNumber *n = [NSNumber numberwithint:100];
NSString *s = [NSString stringwithformat:@ "Jack"];
NSString *s2 = @ "Rose";
8.1 How do these systems have their own methods to produce Autorelease objects?
1> first from the construction method, assuming that there is a book class, book has the price property
-(Instancetype) Initwithprice: (float) price{
if (self = = [Super init]) {
_price = Price;
}
return self;
}
2> Common class method
+ (Instancetype) book{
return [[Self alloc]init]autorelease]
}
3> class method + construct
+ (Instancetype) Bookwithprice: (float) price{
return [[Self alloc]initwithprice:price] autorelease];
}
IOS Autorelease using the detailed