Object C Memory Management

Source: Internet
Author: User

Object C Memory Management
I. Basic concepts:

1. the Objective-C memory management mechanism in the iPhone system is flexible, that is, it can be used like C/C ++, you can also add an AutoreleasePool to upgrade it to a semi-automated memory management language;
2. Reference count is the only reference for memory collection of instance objects

RetainCount is the only basis for Objective-C to manage object references. After the instance's release method is called, this attribute is reduced by one. The dealloc method of the object is automatically called when it is reduced to zero and the memory is recycled. That is to say, we should never manually call the dealloc method of the object.

3. "concept of ownership"

1) the right to use an object is known as having this object. The object can only exist if its owner number is at least 1; otherwise, it should be destroyed immediately;

2) method for obtaining the ownership of an object: After alloc, copy, and retain operations are performed on the object;

4. The concept of "reference" d

In the Object-Oriented field, there is a reference concept. Unlike inheritance, references are often used as designs with lower coupling. When an instance has another instance, we call it a reference to another instance.

 

For example, the Setter method of an attribute object in the ClassA class:

-(Void) setMyArray :( NSMutableArray *) newArray {

If (myArray! = NewArray ){

[MyArray setMyArray: nil]; // you don't need to use release to think about why. (The realse method of myArray will be called in the dealloc method of the instance)

MyArray = [newArray retain];

}

}

5. Reference count:

Each object has a retainCount. when an object is created, the reference count is 1;

6. 2

 

Ii. Memory Management API and usage guidelines:

1. alloc: allocates memory for a new object and its reference count is 1. Calling the alloc method gives you ownership of the new object;

2. copy: Create a copy of the object. Change the retainCount value of the copy to 1, and the caller has ownership of the copy;

3. retain: Make retainCount + 1; and Have object ownership;

4. release: Make retainCount-1;

5. autorealse: a future time to make retainCount-1;

6. dealloc: it is automatically called when the system retainCount is 0;

-(Void) dealloc {

[Name release];

[Super dealloc];

} The release sequence of the variable is opposite to the initial sequence;

Iii. Memory Management Principles:

Take 1 2 3 as class A (retainCount + 1 );

Class B: retainCount-1 with 4, 5

1. For the same object, the call times of A and B are consistent;

2. Any way to obtain the ownership of an object through alloc, retain, copy, or other means; you must call release or autoRelease for release if not applicable;

3. Do not release objects that do not belong to you;

4. autorelease only means delayed sending of a release message;

5. You do not need to release the constructor or accessor because you have not obtained the right to use the object;

4. small examples:

1.

Person * person1 = [[Person alloc] initWithName: @ "James"];

NSLog (@ "name is % @", person1.name); // assume that we have never used person1 since then and should release the object.

[Person1 release];

2.

Erson * person2 = [Person alloc] initWithName: @ ""];

NSString * name = person2.name; NSLog (@ "% @", name); // assume that we will not use person2.

[Person2 release];

// The name should not be released because it is indirectly obtained, so it does not have ownership.

 

3. objects generated by the convenience constructor should not be destroyed by the user, but are completed by the convenience constructor itself.
 

+ (Id) personWithName :( NSString *) aName

{

Person * person = [[Person alloc]

InitWithName: aName];

Return person ;}

① Error, because after the person object is returned, the class loses the opportunity to release the object;

② If [person release] is added before the return statement, it is also incorrect because the object has been destroyed and cannot be used;

③: Correct practice: Add [person autorelease] before the return statement.

 

(2) objects created using the constructor do not need to be released;

For example:

-(Void) printHello

{

NSString * str = [NSString

StringWithFormat: @ "Hello"];

NSLog (@ "% @", str );}

 

4. accessors and seters:

 

1) In the setter, keep the ownership of the new object and discard the ownership of the old object.

 

-(Void) setName :( NSString *) aName {if (name! = AName ){

[Name release]; // if you have any questions, will it cause multiple releases;

Name = [aName retain]; // or copy}

}

2) In the accessors, retain or release is not required.

 

-(NSString *) name {

Return name ;}

 

3) The object obtained by the accessors does not need to be released after use.

 

-(Void) printName {

NSString * name = person. name;

NSLog (@ "% @", name );}

 

5. Common Errors:
 

1) No setup is used.

-(Void) reset {

NSString * newName = [NSString alloc]

InitWithFormat: @ "theNew"]; name = newName;

[NewName release];}

 

2) Memory leakage

 

-(Void) reset {

NSString * newName = [NSString alloc]

InitWithFormat: @ "theNew"];

[Self setName: newName];

}

 

3) release objects without ownership

-(Void) reset {

NSString * newName = [NSString

StringWithFormat: @ "theNew"];

[Self setName: newName];

[NewName release];

}

Related Article

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.