Objective-c (16, memory management, auto-release pool, ARC, strong pointer, weak pointer, method family)--ios Development Basics

Source: Internet
Author: User

Combining the previous study notes and referring to the full solution of Objective-c Programming (third edition), this paper summarizes the objective-c knowledge points. Knowledge points have been changing, just as a reference to the official Apple document ~

16. Knowledge of memory management (II.)

1, autorelease, automatic release mechanism
-(instancetype) autorelease;
(1) Automatic release of pool creation
Before iOS5.0

*pool//进行一系列操作 //此处不可以使用break、return、goto*p = [[[Person alloc] init] autorelease]; [pool release];

After iOS5.0

@autoreleasepool//进行一系列操作//此处可以使用break、return、goto等语句//代码 };

in Arc, the NSAutoreleasePool is disabled , but the new syntax is used for better performance and higher efficiency.

(2) Basic usage
A, the object that calls the Autorelease method is put into the automatic release pool;
b, when the pool is destroyed, all objects recorded in the pools are sent a release message ;
C, call the Autorelease method does not affect the reference counter count value, returns the object itself;

(3) Advantages and disadvantages
Pros: Don't worry about when an instance object is released, don't worry about when to call release
Disadvantage: The object release time cannot be controlled precisely , and if it is a large object, it should be released immediately, otherwise it will consume memory.

(4) Storage mode
A, the data structure of the stack to store, release to follow the advanced ;
b, the iOS program runs, allows to create multiple pool, can be nested using

@autoreleasepool {     @autoreleasepool {         //XXXX     } }

C, when an instance object calls the Aurelease method, this instance object is placed in the top-of-stack auto-release pool.

(5) in the actual development, often create the class method to quickly create objects

 + (id)person {     return [[[self alloc] init] autorelease]; }

Note: do not specify a type, use self, which enables both the creation of a parent class object and the creation of a subclass object
usually the system comes with a method without the Alloc,new,copy keyword, indicating that the returned instance object is Autorelease, no need to manually call release

2, arc:automatic Reference counting, automatic reference counting
(1) Basic features
A, a compiler feature, Xcode5.0 the subsequent creation of the project automatically execute the ARC mechanism;
b, the compiler will automatically add retain/release/autorelease related code in the location of the release based on the assignment operation, the initial change of the variable, the life cycle of the variable, etc.
C,Arc can only manage OC objects and cannot manage the memory requested by malloc

(2) Criteria for arc Judging whether the object should be released:
As long as there is no strong pointer pointing to this object, it will be released;

(3) Note the main points:
A, arc effectively in the program, can not invoke the reference count-related methods Retain,release,autorelease,retaincount
Nor can the selector (@selector (retain)) that use such functions.
b, can rewrite Dealloc, but cannot call [super Delloc];
C,@property related parameter modification
Strong: Member variable is a strong pointer type, equivalent to retain in MRC (used for OC object)
Weak: weak pointer type, equivalent to assign in MRC (used for OC objects)
Assign: Non-pointer type (for non-OC objects)
Unsafe_unretained: Weak pointers for non-nil
(4) Circular reference: one end with strong, one end with weak

3, strong pointer, weak pointer (strong reference, weak reference)
(1) Strong pointer: __strong, with ownership of the pointer

(2) Weak pointer: __weak, does not affect the release of the object
A, in order to avoid circular references, it is necessary for such variables to be able to reference objects, but not to be object owners, not to affect the object itself and reclaim
b, a weak pointer is created by storing a pointer to an object, and the object is not preserved
C, weak references, regardless of the variable assignment or dereference, the variable reference count will not change;

(3) __unsafe_unretained : Non-nil weak pointer, that is, when the point of the memory area is released, the pointer is a wild pointer;
A, as the literal meaning, this type of variable is unsafe, the improper operation of the wild hands can cause serious consequences;
b, some instances of the class cannot use the weak reference of auto-nil, need to use __unsafe_unretained instead of __weak
For example, instances of Nswindows, Nstextview, Nsfont, Nsimage, and so on in lion Releasenote cannot use weak references, but it is unclear whether future support is forthcoming, or whether the future of the current auto-nil will continue to support.
Add the above modifiers to the object before using
For example:__unsafe_unretained Person* p;

4 . Automatic nil of weak pointer
The weak pointer automatically becomes nil when the instance object it points to is freed;
That is, if the instance object pointed to by the weak pointer is freed, the weak pointer does not become a wild pointer

5. Method families: Collection of methods related to object generation
the following naming conventions need to be met (must be followed, otherwise arc may not release objects, causing memory leaks)
--The selector is the same as the method family name (_ can be ignored at the beginning), or the name of the selector is composed of a string that starts with the method family name plus a non-lowercase letter
For example

initinitWithNameinitToMemory _init:locale

Error:

 initialize     init后面不可接小写字母 initwithName   do_init        不以init开头

Currently OC has 5 method families

copynew方法族   以上4种以其开头的方法表示调用者对被创建的对象拥有所有权,返回的对象必须是可以被retain的 init方法族

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Objective-c (16, memory management, auto-release pool, ARC, strong pointer, weak pointer, method family)--ios Development Basics

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.