IPhone/MAC objective-C memory management tutorial and Principle Analysis (2) tips and Paradigm

Source: Internet
Author: User

Copyright Notice

This article is copyrighted by Vince yuan (Vince. Yuan # gmail.com. Welcome to non-profit reprint, reprint must contain the original link http://vinceyuan.cnblogs.com, and must contain the complete content of this copyright statement.

 

 

Version 1.1 was published on

 

Tips and Paradigm

1 tip.

1.1 who created and who released (similar to "Who polluted and who managed "). If you create an object through alloc, new, or copy, you must call release or autorelease. In other words, if it is not created by you, you do not need to release it.
For example, if alloc generates an object in a function and the object is only used in this function, you must call release or autorelease in this function. If you alloc a member object in a class method and do not call autorelease, you need to call release in the dealloc method of this class; If autorelease is called, you do not need to do anything in the dealloc method.

1.2 all objects created by methods except alloc, new, or copy are declared as autorelease.

1.3 who retain and who release. If you call retain, you must call release no matter how the object is generated. Sometimes there is no retain in your code, but the system will add it to the default implementation. I don't know why Apple's documents didn't emphasize this very important point. Please refer to Chapter 2.7 and chapter 3 of paradigm.

2 paradigm.
The paradigm is the template. Because different people have different understandings and habits, the paradigm I have summarized may not be suitable for everyone, but I can ensure that there will be no problems in doing so.

2.1 create an object.

Classa * obj1 = [[classa alloc] init];

2.2 create an autorelease object.

Classa * obj1 = [[classa alloc] init] autorelease];

2.3 release an object and immediately clear the pointer. (By The Way, release is legal as a null pointer, but nothing will happen)

[Obj1 release];

Obj1 = nil;

2.4 assign a pointer to another pointer.

Classa * obj2 = obj1;

[Obj2 retain];

// Do something

[Obj2 release];

Obj2 = nil;

2.5 create and return an object in a function. You need to set this object to autorelease

Classa * func1 ()

{

Classa * OBJ = [[classa alloc] init] autorelease];

Return OBJ;

}

2.6 call the dealloc method of the base class in the dealloc method of the subclass

-(Void) dealloc

{

...

[Super dealloc];

}

2.7 create and use property in a class.

2.7.1 declare a member variable.

Classb * objb;

2.7.2 declare property and add the retain parameter.

@ Property (retain) classb * objb;

2.7.3 define property. (For the default implementation of property, see Chapter 3)

@ Synthesize objb;

2.7.4 apart from the dealloc method, property is always called using the. Operator.

Self. objb or obja. objb

2.7.5 release the member variable in the dealloc method.

[Objb release];

The sample code is as follows (For details, refer to the memman-property.m in the attachment, and you need to pay special attention to when the object is destroyed .) :

@ Interface classa: nsobject

{

Classb * objb;

}

 

@ Property (retain) classb * objb;

@ End

 

@ Implementation classa

@ Synthesize objb;

-(Void) dealloc

{

[Objb release];

[Super dealloc];

}

@ End

2.7.6 when assigning values to this property, there are two methods: manual release and autorelease.

Void funcnoautorelease ()

{

Classb * objb1 = [[classb alloc] init];

Classa * obja = [[classa alloc] init];

Obja. objb = objb1;

[Objb1 release];

[Obja release];

}

 

Void funcautorelease ()

{

Classb * objb1 = [[classb alloc] init] autorelease];

Classa * obja = [[classa alloc] init] autorelease];

Obja. objb = objb1;

}

Sample Code File Link: http://files.cnblogs.com/VinceYuan/objective-c-memman.zip

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.