When using afnetworking, the controller clicks back to destroy, but still performs a block that succeeds or fails the request, causing a wild pointer exception

Source: Internet
Author: User

Originally I thought it was my program framework has a problem ... Later only to know that ignorance is really scary ...

__unsafe_unretained __block typeof (self) weakself = self;    Afhttpsessionmanager *manager = [Afhttpsessionmanager manager]; Manager.responseSerializer.acceptableContentTypes = [Nsset setwithobjects:@ "Application/json", @ "Text/json", @ "text    /javascript ", @" text/html ", nil];        Manager.requestSerializer.timeoutInterval = 30; [Manager post:urlstring Parameters:params constructingbodywithblock:block progress:uploadprogress success:^ (                Nsurlsessiondatatask * _nonnull task, id _nullable responseobject) {if (responseobject) {                                    Manipulating fixed data [weakself operationwithsessionid:responseobject[@ "SessionID"]                                      userinfo:responseobject[@ "UserInfo"] shopcartnum:responseobject[@ "Shopcartnum"]                                secret:responseobject[@ "Secret"];        Success (task, responseobject);            } else {[Svprogresshud dismiss]; RPlog (@ "There is a problem with the requested link");    }} failure:^ (Nsurlsessiondatatask * _nullable task, Nserror * _nonnull error) {Failure (task, error); }];

As we all know, using self in block will form a circular reference, so I use __unsafe_unretained __block typeof(self) here Weakself = self; to avoid,

But after each request (the result of the request has not been returned), I click Return on the navigation bar, there will be a wild pointer exception ... I was wondering ... The controller has also been destroyed ... Why would I call block after the request was completed?

In other words, the controller was destroyed too early ... So the block after the request to access the wild pointer ...

Then I went blind to a method, the method is as follows:

This problem occurs because the controller is destroyed too early. So I'm going to use self instead of weakself in the block, let block make a strong reference to self, and then destroy the block after the request is complete, and the controller will destroy it.

But the disadvantage of this is that it will lead to memory leaks, although I in the test, the request sent out, point back, request completion, the controller also destroyed ... I guess it might be afnetworking. This framework optimizes the life cycle of blocks after a request is completed.

Notice, I said I guess!! I don't know if that's true.

Although the problem has been solved temporarily ... But the heart is still very difficult to suffer ... Because it always feels like something is wrong ... So there's a second way to solve it ...

is __unsafe_unretained __block typeof(self) weakself = self; __weak typeof(self) weakself = self ; the problem is solved!!!

Then I found the difference between the two on the Internet.

Specific can see here ~ ~ ~

Http://www.it165.net/pro/html/201410/24001.html

When using afnetworking, the controller clicks back to destroy, but still performs a block that succeeds or fails the request, causing a wild pointer exception

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.