iOS Development--circular reference problem, common control why use weak, agent why use weak,block inside the outside of the east

Source: Internet
Author: User

All reference counting systems have problems with cyclic applications.

For example, the following reference relationship object:

A was created and referenced to object B.

Object B was created and referenced to object C.

Object C was created and referenced to object B.

At this time, the reference counts for B and C are 2 and 1, respectively. When a no longer uses B, the call release releases the ownership of B, because C also references B, so the reference count for B is 1,b and will not be freed. B is not released, the reference count of C is 1,c and will not be released. From then on, B and C are always left in memory, causing memory waste. In this case, the circular reference must be interrupted and the reference relationship maintained through other rules.

So why do ordinary controls use weak:

if the view in the controller is passed [self. View ADDSUBVIEW:BTN]; BTN has been added strong references, so will not be destroyed, so if you want to take btn as a property, there is no need to The property uses strong references to waste resources.

If you want to use a strong reference can also, because after the controller is destroyed, the controller view is also destroyed, so BTN destroyed, will not cause a strong reference.

Why should the agent use weak such as:

Assuming that the proxy is set to strong then the statement Uiscrollview*scrollview=[uiscrollview Alloc]init];scrollview.delegate=self is executed, and (self represents the controller)

When the controller wants to destroy, the controller is pointed by a strong pointer, and ScrollView added to the controller view is also pointed by a strong pointer, can not be destroyed, then its delegate will not be destroyed. The delegate points to the controller. causes a circular reference. Turn delegate into weak: When the controller life cycle ends, the view is destroyed, and the internal subspace is destroyed, and delegate is destroyed.


Inside the block, you need to convert the outer object into a weak pointer, otherwise it will cause circular references such as:

With this piece of code, the Code does not have to understand: Mail has a block property, and a mail controller in the block needs to set up a proxy.

The Shareviewcontroller internal strong pointer points to the mail object with the items strong pointer inside the Group,group, the Mail object has an option attribute (block), and option internally uses the self is the controller, So block (option is copy type) will point to self with a strong pointer. Note: This is not the same as above, do not be the VC agent (agent is weak) is confused, is the block strong pointer to self. Cause a circular reference no one can be destroyed.

Workaround: use:

__unsafe_unretained typeof(self) SELFVC =self;

Or

__unsafe_unretained hyshareviewcontroller *SELFVC =self; let him only have weak references to self in block.

Or change the __unsafe_unretain into __weak. (the difference between __unsafe_unretain and __weak, see the next <OC syntax--__unsafe_unretain, __strong, __weak, __autoreleasing these four properties of the role > )


    Hysettingitem*mail = [HysettingarrowitemItemwithicon:@ "Mailshare"title:@"Email Sharing"Destvcclass:Nil];

Mail. option = ^{

// Cannot send mail

if (![ Mfmailcomposeviewcontrollercansendmail])return;

&NBSP;*VC = [[[ Mfmailcomposeviewcontroller alloc init Span style= "Color:rgb (0, 0, 0); >];

        [Vcsetsubject : @ " Conference "];

: @ " > We have a meeting this afternoon. " ishtml : no

Vc. mailcomposedelegate = SELFVC;

// Display Controller

[SELFVC presentviewcontroller: VCanimated:YEScompletion: Nil ];

};

hysettinggroup *group = [[hysettinggroupalloc]init];

Group. items =@[sina, sms, mail];

[Self. Data addobject: Group];

}


iOS Development--circular reference problem, common control why use weak, agent why use weak,block inside the outside of the east

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.