iOS Review notes 8:autorelease detailed

Source: Internet
Author: User



A concept
iOS in the process of running the program, will create a number of free pools, the automatic release of the pool in the form of a stack (advanced back out).
When an object calls Autorelease, it is placed in the auto-free pool at the top of the stack.
When the auto-free pool is destroyed, a release message is sent to all objects of the pool;
So the reference count does not immediately-1 after sending autorelease.
Autorelease returns the object itself.


Two examples
Person.h@interface Person@property (nonatomic, assign) int age; @end

Person.m#improt <Foundation/Foundation.h> @implementation person-(void) Dealloc () {NSLog (@ "Person dealloc"); [ Super Dealloc];} @end

Main.m#improt <Foundation/Foundation.h> #improt "Person.h" int main () {@autorelease {//created an auto-release pool person* Person1 = [[Person alloc] init] autorelease];p erson1.age = 25;} Release pool destroyed, this line will print out: "Person dealloc"//Free pool number @autorelease{person* person2 = [[[Person alloc] init] autorelease]; Person2.age = 25;//Free pool can be nested @autorelease{person* person3 = [[Person alloc] init] autorelease];p erson3.age = 25;}} Another way to create nsautoreleasepool* p = [[NSAutoreleasePool alloc] init];//creates an auto-free pool person* person4 = [[[Person alloc] init] au Torelease];p erson4.age = 25; [Pool release];//release tank destroys return 0;}


The visible release pool is created in two ways:
Way One
@autorelease {//release in-Pool object}

Way Two

nsautoreleasepool* p = [[NSAutoreleasePool alloc] init];//created an auto-release pool//release in-Pool object [pool release];




Three advantages Disadvantages:
1 advantages
No more worrying about whether the call is placed before release;
Do not care if release is again after all calls.


2 Disadvantages:
The object release time cannot be controlled precisely;


Therefore, it is recommended to use Autorelease for objects that consume less memory


Four Common mistakes
1 Multiple calls to Autorelease
person* Person2 = [[Person alloc] init] autorelease]; [Person2 Autorelease];
Or
person* Person2 = [[[Person alloc] init] autorelease] autorelease];

2 call release after calling Autorelease
person* Person2 = [[Person alloc] init] autorelease]; [Person2 release];


Automatic release optimization for five classes of methods

Person.h@interface Person@property (nonatomic, assign) int age;+ (ID) person;+ (ID) personwithage: (int) age; @end

Person.m#improt <Foundation/Foundation.h> @implementation person//quickly returns an automatically freed object + (ID) Person{retur [person ALLOC] init] autorelease];} + (ID) personwithage: (int) age{person* p = [Person alloc] init] autorelease];p. Age = Ageretur p;} @end

Main.m#improt <Foundation/Foundation.h> #improt "Person.h" int main () {@autorelease {person* p1 = [person person] ;p 1.age = 22; person* P1 = [person personwithage:25];//similar system method nsstring* str = [NSString stringWithFormat ("%d to string", 2)]; nsnumber* n = [NSNumber numberwithint:100];} Retur 0;}



Optimization of Class six methods
1 Problem Example
From a new definition of a class student inherit from person
Student.h@interface Student:person@property (nonatomic, assign) int grade; @end

Student.m#improt <Foundation/Foundation.h> @implementation Student@end


Then call the following method
Main.m#improt <Foundation/Foundation.h> #improt "Person.h" int main () {@autorelease {student* s = [Student Personwithage:10];s.grade = 1;//error}retur 0;}


You can see the error in S.grade = 1;
[Person setgrade]:unrecognized selector sent to instance ...
Why is it? Because student's personwithage is inherited from the parent class person, it returns a person object,
The person object does not have a grade attribute.


2 workaround
Modifies the class method in the parent class person, changes the person to self, and returns the object of the calling class.


+ (ID) person{//Returns the caller's instance retur [self alloc] init] autorelease];} + (ID) personwithage: (int) age{person* p = [Self alloc] init] autorelease];//set the age value of the parent class (person) p.age = Ageretur p;}


iOS Review notes 8:autorelease detailed

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.