2-23 the ownership modifier (_strong modifier and _weak modifier) in Arc

Source: Internet
Author: User

A _strong modifier

The 1._strong modifier is the default ownership modifier for the ID type and object type. As follows:

ID obj = [[NSObject alloc] init]; // when no explicit variable ownership modifier is used, the default is _strong modifier ID _strong obj = [[NSObject alloc] init]; // The code above is the same. 

2. As shown in the name strong, the _strong modifier represents a strong reference to an object. Variables that hold strong references are discarded when they are outside their scope. With the invalidation of a strong reference, the referenced object is invalidated.

Use a piece of code to illustrate, as follows:

{    id _strong obj = [[NSObject alloc] init];   Build and hold the object yourself. /    *     because the variable obj is a strong reference to    all of its own    objects */}    /* The      variable obj exceeds its scope and the strong reference is invalidated.    so automatically release the objects that you hold.    The object's owner does not exist, so the object is discarded.     */

The above is an example of the objects that are generated and held by themselves, as well as when acquiring non-self-generated and holding objects.

3. Take a look at the complex build and hold the strong application of the object.

   ID_strong obj0 = [[NSObject alloc] init];//Object A    /*Obj0 holds a strong reference to object a*/    ID_strong obj1 = [[NSObject alloc] init];//Object B    /*Obj1 holds a strong reference to object B*/    ID_strong Obj2 =Nil;/*Obj2 does not hold still and object*/obj0=obj1; /*Obj0 holds a strong reference to object B that is assigned by OBJ1 because Obj0 is assigned a value, so the owner of the previously held object A's strong reference to invalid object A does not exist, so the object is discarded. The strong reference to the B object changes to Obj0 and obj1 at this time*/

By the above example, we can find the variables of the _strong modifier, not only in the scope of the variable, but also in the assignment of the correct management of its object su has.

Two _weak modifiers

To understand the role of the _weak modifier, first understand the problem of circular references brought by the _strong modifier, as follows:

Suppose you already have a class test that inherits from NSObject and has a member variable, obj also inherits from Nsobect. There is a problem with the following circular reference.

{    IDTest0 = [[Test alloc] init];//Object A//Test0 holds a strong reference to object a    IDTest1 = [[Test alloc] init];//Object B//Test1 holds a strong reference to object B[Test0 Setobject:test1]; /*The member variable of object A, obj, holds a strong reference to test object B for the strongly referenced object B to test1 with the member variable obj of object A. */[Test1 setobject:test0]; /*the member variable of object B, obj, holds the strong reference to test object a strongly referencing object A to test0 with the member variable obj of object B. */ } /*because the TEST0 variable exceeds its scope and the strong reference is invalidated, the test object A is automatically freed because the Test1 variable is out of scope and the strong reference is invalidated, so the automatic release of the test object B is now a variable that holds the strong reference to object A: the member variable of object B, obj.  The variable that holds the strong reference to object B is the member variable obj of object A.  Both object A and object B have lost their role and should be discarded, but not discarded. A memory leak has occurred! */

Circular references are prone to memory leaks, so-called memory leaks mean that objects that should be discarded persist beyond their lifetimes.

As in this case, there is only one object, but a circular reference occurs when the object holds itself.

    ID _strong obj = [[NSObject alloc] init];    [obj setobject:obj];

2. To avoid circular references, use the _weak modifier. The _weak modifier, in contrast to the _strong modifier, provides a weak reference. A weak reference cannot hold an object instance.

{    id _strong obj0 = [[NSObject alloc] init];   Build and hold the object yourself.     ID _weak obj1 = obj0;   Obj1 holds a weak reference to the object.  }/*obj0 out of its scope, the strong reference fails, so the object that you hold is automatically freed. This object is discarded because the owner of the object does not exist. */

Because a variable with the _weak modifier (that is, a weak reference) does not hold an object, the object is freed when its scope is exceeded. A circular reference can be avoided if the variable of the class that preceded the circular reference has been changed to a member variable with the _weak modifier.

2-23 the ownership modifier (_strong modifier and _weak modifier) in Arc

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.