IOS arc mechanism

Source: Internet
Author: User

iOS provides ARC functionality that greatly simplifies the memory management code.

1, the essence of Arc:

With ARC, iOS developers can completely abandon the cumbersome memory management mechanism. In the case of arc opening, the compiler will automatically insert Retain,release and autorelease in the correct location of the program. Specifically, ARC is only a feature of the Objective-c compiler, and all arc related processing occurs when the application is built, unlike the memory garbage collection mechanism.

2. Basic ARC Usage Rules

1) The code can not use Retain,release,autorelease, etc.;

2) object pointers cannot be used in C-language structures;

3) Dealloc can not be overloaded, if the object memory is disposed outside of processing, it is possible to overload the function, but cannot call [super Dealloc];

4) You cannot use NSAutoreleasePool, but use @autoreleasepool blocks

3. @property rules under the ARC mechanism

Strong: The property value corresponds to the __strong keyword, that is, the variable declared by the attribute becomes the holder of the object.

Weak: This property corresponds to the __weak keyword, the variable declared by this property will not have ownership of the object, and when the object is discarded, the object will be automatically copied nil;

Copy: The difference from strong is that declaring a variable copy is the holder of the object.

Turn from Cool bar: http://www.tekuba.net/program/279/

However, using arc does not mean that memory leaks do not occur, and memory leaks can occur if used improperly.

One, circular reference

A There is an attribute reference b,b has an attribute reference a, and if all are strong references, none of the two objects can be freed.

This problem often occurs when the delegate is declared as a strong attribute.

1 @interface Sampleviewcontroller 2 @property (nonatomic, Strong) SampleClass *SampleClass; 3 @end 4 5 @interface SampleClass 6 @property (nonatomic, Strong) Sampleviewcontroller *delegate; 7 @end

In the above example, the solution is to change the SampleClass delegate property strong to Assing.

Two, the cycle of death

If there is an infinite loop in a viewcontroller, it can also cause the Viewcontroller to be released even if the viewcontroller corresponding view is switched off.

This problem often occurs in animation processing.

1 catransition *transition = [catransition animation]; 2 0.5 ; 3 tansition.repeatcount = huge_vall; 4 [Self.view.layer addanimation:transition forkey:"myanimation"];

In the above example, the number of animation repeats is set to Huge_vall, and a large number is basically equal to the infinite loop.

The solution is to stop the animation when the Viewcontroller is turned off.

-(void) viewwilldisappear: (BOOL) animated {    [Self.view.layer removeallanimations];}

 

IOS arc mechanism

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.