ARC mechanism, arc

Source: Internet
Author: User

ARC mechanism, arc

Concepts and Principles of ARC

1. Understand pointer Classification

(1) strong pointer: by default, all pointers are strong pointers, and the keyword strong (2) weak pointer: _ weak Pointer Modified by the keyword
Declare a weak pointer as follows:
_ Weak Person * p;

2. What is ARC?

Automatic Reference Counting, the Automatic Reference Counting, that is, ARC, is the biggest change and most exciting change introduced by WWDC2011 and iOS5. ARC is a feature of the new LLVM 3.0 compiler. Using ARC can be said to solve the trouble of manual memory management hated by iOS developers. Using ARC in projects is very simple: you only need to write code as usual, retain, release autorelease ~ This is the basic principle of ARC.

When ARC is enabled, the compiler will automatically insert retain and releaseautorelease where the code is appropriate. As a developer, you do not need to worry about the wrong Compiler (unless the developer mistakenly uses ARC ).

Manual memory management (MRC)

ARC is different from the "Garbage Collection" mechanism in other languages. ARC: Compiler features: "Garbage Collection" RunTime features

As long as an object is directed by any strong pointer, it will not be destroyed.

3. Working Principle and judgment criterion of ARC

ARC is a feature of the Objective-C compiler, rather than a runtime feature or garbage collection mechanism. What ARC does is to automatically insert release or autorelease at the right position during code compilation,

ARCJudgment Criterion: as long as there is no strong pointer to the object, the object will be released.

Note: When Using ARC, you forget to reference the counter because the judgment standard has changed.

 

1) All pointers with strong pointers are strong pointers by default.

Use _ strong as a strong pointer

2) all weak pointers are strong pointers by default.

Weak pointer identified by _ week

 

Circular references under ARC

Person. h

# Import <Foundation/Foundation. h> @ class Dog;
@ Interface Person: NSObject

// Dog is a strong pointer of strong.
@ Property (nonatomic, strong) Dog * dog;

@ End

Person. m

# Import <Foundation/Foundation. h>
@ Class Person;
@ Interface Dog: NSObject
// The dog's master is also a strong pointer
@ Property (nonatomic, strong) Person * owner; @ end

Main. m

Person * p = [Person new]; Dog * d = [Dog new];

 

WhenP. dog = dAfter,Can still be released normally 

 

WhenD. owner = p;Form a circular reference 

 

Cause loop Introduction 

Solution:
One of the objects introduced by the loop is setStrongAnother setting isWeak

 

Under ARC@PRopertyParameters

@ Property in ARC
Strong: used for OC objects, equivalent to retain in MRC
Weak: used for OC objects, equivalent to assign in MRC
Assign: used for basic data types, the same as assign in MRC.
Copy: generally used for NSString. Similar to the copy in MRC, the "loop retain" problem is solved in ARC: @ property uses strong while weak

 

Features and precautions of ARC

1. Summary of ARC features

(1) release, retain, retainCount cannot be called.

(2) dealloc can be rewritten, but [super dealloc] cannot be called.

(3) @ property parameters:

Strong: equivalent to the original retain (applicable to the OC object type). The member variable is a strong pointer.

Weak: equivalent to the original assign (applicable to the oc object type), the member variable is a weak pointer assign: applicable to non-OC object types (basic type)

2. ARC usage considerations

1) In ARC, as long as the object to which the weak Pointer Points is no longer present, the weak pointer is directly cleared (assigned as nil.

2) _ weak Person * p = [[Person alloc] init]; // unreasonable. Once an object is created, it is released. After the object is released, ARC sets the pointer to nil.

3) in the ARC, the retain is no longer used in the property, but strong is used, and no need
[Super dealloc].
@ Property (nonatomic, strong) Dog * dog;
// Indicates that the generated member Variable _ dog is a strong pointer, which is equivalent to the previous retain.

4) if it is replaced with a weak pointer, it is replaced with weak without adding __.

(See master Chuanzhi Ruyi !)

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.