OBJECTIVE-C Memory Management

Source: Internet
Author: User

Reference: http://blog.csdn.net/hahahacff/article/details/39839571

After watching the video, the article summarizes the OBJECTIVE-C memory management that you understand.

This article from the top to the next step in depth.

ARC

1. Basic operating functions and principles of manual memory management (golden rule or pairing principle)

The memory is opened up and destroyed in pairs.

In a code quickly appeared alloc,new,retain,copy,mutabcopy and other keywords, when the object is no longer used, it is necessary to have corresponding release,autorelease appear corresponding to it.

When the object's reference count (Retaincount) changes to "0", the object executes the Dealloc function, performing the destroy operation.

2. When instantiating an object in a class, we can simply use Alloc and release to solve the pairing problem.

Person *p = [[Person alloc] init];

[P run];

[P release];

3. How should a class be paired when it is the property of another class (held by another class)?

Object Alloc;

To be held; The-----------------is held to execute the Set method: So the object should be retain in the set method, and the object should be in the Dealloc function.

Object release;

PS. Two pairs of memory operations above.

4. What happens when two objects are alternately held by another object?

Because the alternating operation causes a memory leak for the first object that is held.

So immediately after the first held object is not used in this class, release the first object that is held in the set method.

Code:

-(void) Setcar: (Car *) car

{

[_car release];

_car = [car retain];

      

}

  

5. When the same object is held multiple times by another object, it sometimes causes the first object to be released prematurely. (Zombie object, the presence of wild pointers) (the wild pointer is treated by assigning a value of Nil,p = nil) to the wild pointer

The set method above will make an error.

So we need to judge two times that the object being held is not the same object.

The code is as follows:

-(void) Setcar: (Car *) car

{

if (_car! = car)

{

[_car release];

_car = [car retain];

}

}

  

The final dealloc also needs to be rewritten:

-(void) dealloc

{

[_car release];

[Super release];//Last Call, because the parent class will destroy the subclass after it executes the release, the following code will not execute, causing a memory leak. The true destruction of subclasses is performed in the parent class.

In Arc, you can override the Dealloc function, but you cannot write [super release];

}

Use of [email protected]

Designed to simplify the Get,set method when @property. We can completely replace it with the expanded Get,set method.

Related parameters:

Property keyword Scope of Use Interpretation is the default value Tips
Assign How to assign a value Do not copy without reservation, directly assign value YES

Basic data types and objects not directly owned by this class

Retain How to assign a value Keep the new value a copy of the assigned overwrite original value NO Most objects can use
Copy How to assign a value Copy the new value one copy of the assigned overwrite original value NO String selective use
ReadWrite Read and Write permissions Two ways to generate getter and setter YES Variable can be read and modifiable
ReadOnly Read and Write permissions Generate Getter Methods only NO Variable read-only non-modifiable
Atomic Atomic Nature Atomic operation YES Can be kept in multi-threaded environment, safe access to the value
Nonatomic Atomic Nature Non-atomic operation NO Do not generate multithreaded synchronization content
Getter Access method Custom Fetch method NO
Setter Access method Custom Assignment methods NO

NSString need to use copy

@property (nonatomic,copy) NSString *str;

The wording in the agent:

@property (nonatomic,assign) id<findapartment> delegate; Because the proxy object does not actually hold the proxy object, the assign!! must be used here in order to avoid circular referencing of the object

@property (nonatomic,weak) __weak id<findapartment> delegate; In Arc.

Block in the notation:

@property (nonatomic,retain) int (^changecolor) (int a,int b); Mrc

@property (nonatomic,strong) int (^changecolor) (int a,int b); ARC, there seems to be no special%>_<%.

7.autorelease

Autorelease is mainly used in the writing of class methods.

(1) How iOS 5.0 was created before

NSAutoreleasePool *pool=[[nsautoreleasepool alloc] init];

`````````````````

[Pool Release];//[pool drain]; for mac

(2) after Ios5.0, and arc can only use this way!!!!

@autoreleasepool

{// start on behalf of Create auto release pool

·······

}// end represents destruction of the auto-release pool

8. Circular References

Workaround:

1. Declare the class with @class in the header file. References the file in the M file.

2. Workarounds for circular references at both ends

One end using retain, one end using assign(with assign in Dealloc and no more release)

ARC:

Arc added two new martial arts strength: strong and weak

Strong has the same meaning as retain, weak and assign, and the modified attribute variable usage is completely unchanged, but strong and weak can only modify objects.

Apple's official memory reference rules for objects in the arc mechanism:

(1) Any object, if there is still a holder, will not destroy

(2) Any object that has not been held by any holder, i.e. automatically destroyed

The holder is a pointer to the object, if it is strong decorated, that is the object holder, if it is the weak attribute, it is not the holder

Egg: Make the program compatible with arc and non- arc parts. Transition to non- arc-fno-objc-arc to ARC , -f-objc-arc .

OBJECTIVE-C Memory Management

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.