Release of circular references in iOS

Source: Internet
Author: User

A: Why do we create circular references?????????????????????????????????????????

First of all

For the Network tool class Finishedblock, one of the properties is copy.

Then Networktools will make a strong reference to the network tool Finishedblock

The controller on the left Viewcontroller inside.

Viewcontroller strong references to Networktools object tools

On the right there is a self.finishedblock = finished assignment

Self.view on the left, then Self.finishedblock = finished this pair Self.view

Make a strong reference

caused a closed-loop finishedblock (Reference)----self.view ()-(self is the controller to Networktools object Tools strong Reference)----tools (referenced) finshedblock

Creates a closed loop that creates a closed strong reference

--------The result is who can not release

Two: How to break the circular reference?????????????????????????????????????????

Way 1:--Break quote chain Way 2:--use __weak

Mode 1: Can be in the left controller Viewcontroller inside to do an action

Change that self.tools into a

Networktools * Tools make Tools a local variable understand?????

He's naturally out of scope and will automatically release the broken link above without Self.view

That controller's strong reference to him.

This is the principle of breaking the reference chain.

Mode 2: In

Added on the left

Mode 2

__weak typeof (self) weakself = self;

[Self.tools lodadata:^ (NSString *html) {

NSLog (@ "%@%@", Html,weakself.view);

}];

Add something like that and we'll be able to contact the circular reference

This reference chain will also be destroyed by the finishedblock of the Self.view.

The general idea is: to break any link in the circular reference line

Method 3: Use __unsafe_unretained typeof (self) weakself = self;

Method 3

__unsafe_unretained typeof (self), weakself= self;

[Self.tools lodadata:^ (NSString *html) {

NSLog (@ "%@%@", Html,weakself.view);

}];

Three: The deep excavation of __unsafe_unretained and __weak?????????????????????

We take a time-consuming operation in the Network tool class to imitate the network request delay operation

__weak is iOS5.0.

This time Weakself.view printing is null

If the asynchronous operation is not completed, he will release the controller __weak itself is a weak reference

When the asynchronous operation is complete, the self has been freed when it returns to the property and cannot be accessed and the method cannot be called, so the print is null

(So the speed of the click to return different printing results are different)

When using __unsafe_unretained, __unsafe_unretained was IOS4.0 's launch.

The program crashes this MRC error bad memory access wild pointer error

__weak equivalent to weak does not make a strong reference but if the object is freed, the execution address points to nil

MRC The following attributes are held by assign and are not strongly referenced but if the object is freed

Memory address is not changed at this point, the call will show the wild pointer Access

So in the development of general to recommend __weak __unsafe_unretained phase for unsafe

And there's a code that comes out

(Stringself strong reference to weakself the original intention to continue executing the callback after executing the code)

But if it __unsafe_unretained, he's going to crash.

The overall difference:

__weak, self has been freed for nil.

__unsafe_unretained if self has been released, there will be a wild pointer access

You can use strongself inside, but there's no deep, bad egg.

Release of circular references in iOS

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.