简体   繁体   中英

Network request comes back after the controller is popped

I make a network request within a viewcontroller, the simplified code is as below:

- (void)viewDidLoad {

    [super viewDidLoad];

    [self http_request];

}

- (void)http_request {

    dispatch_async(gAsynQueueT, ^{

        NSString *response;

        NSURL *url = [NSURL URLWithString:@"http://www.google.com"];

        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

        sleep(5); //leave some time for poping this controller

        [request startSynchronous];

        NSError *error = [request error];

        if (!error) {

           response = [request responseString];

        }

        dispatch_async(gMainQueueT, ^{

            _data = response;  //_data is an global variable

        });

    });


}

Before the network request coming back, I popped the controller. I think this should lead to memory leak, cause when the network request came back the controller is released. But I found nothing wrong with instrument tool.

So, how to expain this situation. Thanks~

You already used dispatch_async(gAsynQueueT, {...}) and it will run on another thread.

And in block, you didn't use view controller's property so I think there is no reason for memory leak.

block was already dispatched to queue so it isn't related to view controller anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM