__weak How to implement object values automatically set to nil

Source: Internet
Author: User


__weak How to implement object values automatically set to nil 

Before starting to explain the __weak mechanism, let's start with some groundwork

the implementation of ARC

Apple's official note says that arc is "memory-managed by the compiler", but actually only the compiler is not fully competent, and arc also relies on the OC Runtime Library, which means that arc is implemented by the following tools and libraries:

Clang (LLVM compiler) 3.0 or more

OBJC4 OC Run-time library 493.9 or higher

If you follow Apple's instructions and only the compiler manages the memory, then the __weak modifier can also be used in iOS 4

__weak modifier

As we know, the __weak modifier provides a magical public energy.

If a variable reference object using the __weak modifier is discarded, nil is assigned to the variable

Using a variable with the __weak modifier is the use of an object registered to Autoreleasepool.

Let's take a look at its implementation:

{id __weak obj_weak = obj;//obj has been assigned and is of type strong}
/* The compiler's analog code */id obj_weak;objc_initweak (&obj_weak,obj);//Initialize the variable objc_destroyweak with __weak modifier (&obj_weak);// Release the variable

Where Objc_initweak Objc_destroyweak is called the Objc_storeweak function, so the above code can be converted to the following code

ID obj_weak;obj_weak = 0;objc_storeweak (&obj_weak,obj); Objc_storeweak (&obj,0); objc_ The Storeweak function takes the address of obj as a key value, and the address of Obj_weak is stored as a value in the weak table (weak is a hash table).

When releasing an object, what is the program's action at the same time that the object is discarded? Object is released through Objc_release.

1. Objc_release

2. Execute Dealloc because the reference count is 0

3. _objc_rootdealloc

4. Object_dispose

5. Objc_destructinstance

6. objc_clear_deallocating

Instead, the action to invoke Objc_clear_deallocating is as follows:

1. Get the address of the discarded object from the weak table as a record of the key value.

2. Assign a value of nil to all addresses that are included in the record with the __weak modifier variable

3. Deleting records from the weak table

4. Delete the address of the discarded object from the reference count table as a record of the key value

According to the above steps, if the object referenced by the variable with the __weak modifier is discarded, then nil is assigned to the variable, and this function is implemented.

The second function of __weak, a variable that uses the __weak modifier, is to use an object registered with Autoreleasepool.

{id __weak obj_weak = obj;//obj has been assigned and is a strong type of NSLog (@ "%@", Obj_weak);}

The code translates to the following form:

/* The compiler's analog code */id obj_weak;objc_initweak (&obj_weak,obj); ID tmp = objc_loadweakretained (&obj_weak); objc_ Autorelease (TMP); NSLog (@ "%@", TMP); Objc_destroyweak (&obj_weak);

The call to the objc_loadweakretained function and the Objc_autorelease function is increased when using the __weak modifier variable when compared to the assigned value. The actions of these functions are as follows:

1. The objc_loadweakretained function takes out the object referenced by the __weak modifier variable and retain

2. The Objc_autorelease function registers the object with the Autorelease.

Thus, because the object referenced by the __weak modifier variable is registered in the autorelease, it is safe to use before the end of the @autoreleasepool block.

Note: There are classes in OC that do not support arc, such as the Nsmachport class. You can use the Allowsweakreference/retainweakreference method to determine whether arc is supported

  compiler how to get to Apple first

__weak How to implement object values automatically set to nil

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.