ARC ———— Auto Reference count

Source: Internet
Author: User

One. Memory Management/reference count

1. How to reference count memory management (four)
Object operation OC Method
generate and hold object Alloc/new/copy/mutablecopyd etc.
Hold object Retain method
Release object R Elease method
Obsolete Object Dealloc method
These methods of memory management are not actually included in the language, but are included in the Cocoa Framework for OS x,ios application development. The NSObject class of the Foundation Framework library in the
Cocoa Framework assumes the responsibility for memory management. The Alloc, retain, release, Dealloc methods in the
OC Memory management refer to the Alloc class method, retain instance method, release instance method, and Dealloc instance method of NSObject class respectively.
A. Self-generated objects that hold themselves.
AA. The method name that starts with the following name means that the object that you generate is only owned by itself: Alloc, New, copy, Mutablecopy.
AB. [[NSObject alloc] init] = [NSObject new];
AC. The Copy method uses the Nscopying method convention to generate and hold a copy of the object by means of the Copywithzone: method implemented by each category.
AD. In the Mutablecopy method, a copy of the object is generated and held by the various implementations of the Mutablecopywithzone: method based on the Nsmutablecopying method Convention.
B. You can hold the object that you are not holding.
ID obj = [nsmutablearray array];
[obj retain];
through the Retain method, objects that are not held by themselves are the same methods that they hold as they are generated and held by the above method.
C. Release the
CA when you no longer need the objects you hold. The holder is obligated to release the object once it is no longer needed. Release using the release method.
CB. It can also be released using the release method if the Retain method becomes self-possessed, rather than the object it holds.
D. Cannot release objects that are not owned by themselves

2.alloc/retain/release/dealloc implementation
The integer value of the reference count is stored in the OC object.
When the Alloc or retain method is called, the reference count value is added to 1.
When release is called, the reference count value is reduced by 1.
When the reference count value is 0 o'clock, the Dealloc method is called to discard the object.
3. Apple's implementation
4.autorelease and its implementation
How to use Autorelease:
A. The NSAutoreleasePool object that generates and holds the object.
B. Invokes the Autorelease instance method of the allocated object.
C. Obsolete NSAutoreleasePool methods.

********************************
Two. ARC rules
1. Overview
To set up an arc-efficient compilation method:
Use the clang (LLVM compiler) version 3.0 or later.
Specifies that the compiler property is "-fobjc-arc".
2. How to think about memory management
And the same as above.
3. Title Modifiers
A. In order to process objects in OC programming, variable types can be defined as ID types or various object types.
The ID type is used to hide the class name portion of an object type, which is equivalent to the commonly used "void *" in C.
The object type is a pointer to an OC class such as nsobject, such as "NSObject *".
B. When arc is valid, the ID type and object type are different from other types of C, and the ownership modifier must be attached to the type. There are four types of ownership modifiers: _strong/_weak/_unsafe_unretained/_autoreleasing.
C. _strong modifier
Ca. The _strong modifier is the default ownership modifier for the ID type and object type.
Cb. The _strong modifier represents a "strong reference" to an object. A variable that holds a strong reference is discarded when it goes out of scope, and the referenced object is freed as the strong reference expires.
Cd. Variables with _strong modifiers can be assigned to each other.
Ce. The _strong modifier can be used on OC class member variables and method parameters.
Cf. The _strong modifier, together with the _weak/_autoreleasing modifier, guarantees that the automatic variables attached to these modifiers are initialized to nil.
such as: ID _strong obj0; = = id _strong obj0 = nil;
D._weak modifier
Da Member variables that use the _strong modifier are prone to circular references when holding objects.
Db. Circular references are prone to memory leaks. A memory leak is an object that should be discarded to persist beyond its lifetime.
dc Use the _weak modifier to avoid circular references.
The DD, _weak modifier has another advantage. When holding a weak reference to an object, if the object is discarded, the weak reference is automatically invalidated and is in a state where nil is assigned (a null weak reference).
De In summary, use the _weak modifier to avoid circular references. By checking whether a variable with the _weak modifier is nil, you can determine whether the assigned object is obsolete.
Df. However, the _weak modifier can only be used for applications that are more than ios5 and above the OS X Lion version. The _unsafe_unretained modifier can be used instead in iOS4 and OS X Snow Leopard applications.
E. _unsafe_unretained modifier
F. _autoreleasing modifier
Fa ARC is valid when the Autorelease method is not available and cannot use the NSAutoreleasePool class. However, the Autorelease feature works.
Fb.id pointers or pointers to objects are appended with the _autoreleasing modifier when they are not explicitly specified.
Fc. To get the details of the error, a pointer to the Nserror object is often passed in the parameters of the method, rather than the function return value.

4. Rules
A. You cannot use retain, release, Retaincount, Autorelease.
A1. Memory management is the work of the compiler, so there is no need to use memory management methods (retain, release, Retaincount, Autorelease).
A2. You cannot use Nsallocateobject, Nsdeallocateobject.
A3. Method naming rules for continuous compliance with memory management
A4. Do not show call Dealloc.
A5. Replace NSAutoreleasePool with @autorelease blocks.
A6. The zone (Nszone) cannot be used.
A7. Object type variable cannot be a variable of C language struct.
A8. Displays the conversion ID and void*.
5. Properties
A. The properties of the OC class also change when arc is valid
B. Property declares the corresponding relationship between the property and the ownership modifier:
Assign ———— _unsafe_unretained
Copy ———— _strong
Retain ———— _strong
Strong ———— _strong
unsafe_unretained ———— _unsafe_unretained
Assigning values to the properties specified above is equivalent to assigning to a variable that has the ownership modifier corresponding to the attached property.
Where only the Copy property is not a simple assignment, it assigns the object generated by the Copywithzone: method of the Nscopying interface to copy the assignment source.
**************************
Three. The implementation of Arc
ARC is "memory-managed by the compiler", but in fact only the compiler is not fully competent, on this basis, the OC Runtime Library is also required to assist. In other words, ARC is implemented by the following tools and libraries:
Clang (LLVM compiler) 3.0 or more
OBJC4 objective-c Run-time library 493.9 or higher.

ARC ———— Auto Reference count

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.