OC Memory management Golden Rule

Source: Internet
Author: User

iOS notes, if you have any errors, please let us know, thank you

1.When you create an object using new, Alloc, or copy, the object has a
Retain count of 1. You is responsible for sending the object a release Orautorelease message when you ' re finished with it. That's the, it gets cleanedup when its useful life are over.

If you create an object with new, Alloc, copy, the object's reference counter is 1. Send the release or autorelease message to the created object when you are finished using it.

2.When you get hold of a object via any other mechanism, assume it has
A retain count of 1 and that it has already been autoreleased. You don't have a further work to make sure it gets cleaned up tneed. If you ' re goingto-on-to-the-object for any length of time, retain it and make sure torelease it when you ' re finished.

Objects obtained by other means, such as the following three lines of code

Person * s = [[person alloc]init];

Person * S1 = s;

[s retain];

S1 a non-new, Alloc, copy method to obtain an object, it is necessary to use retain, release to maintain the use of objects.

3.If you retain a object, you need to (eventually) release or autorelease it. Balance these retains and releases.

Manually manage an object to ensure that retain and release end up as much.

Ps:1. The automatically generated setter, getter method, using a pointer (reference) declared with @property (Nonatomic,retain) is as follows

-(void) Set_student: (student*) student{

if (_student! = Student) {

[_student release];

_student = [student retain];

}

}

-(student*) student{

return _student;

}

The setter and Getter will release the old object, retain the new object, so we use the dot operator, the set method without specifically retain the referenced object

2. Circular reference problem for two objects: @property (nonatomic,assign) to declare a reference to one of the objects.

OC Memory management Golden Rule

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.