从“dispatch_async(dispatch_get_main_queue(),{”块调用委托时内存泄漏

时间:2013-09-21 11:03:28

标签: iphone ios xcode

我想从MyViewController类做异步URL请求(使用UrlRequestor类):

UrlRequestor * urlRequestor = [UrlRequestor alloc] init]];

我将MyViewController类设置为UrlRequestor类的委托:

urlRequestor.delegate = self;

我调用getUrlData函数:

[urlRequestor getUrlData];

UrlRequestor.m文件中的getUrlData函数我为多线程调度队列。它看起来如此:

- (void) getUrlData {
    .......
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
           NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
           ......
           dispatch_async(dispatch_get_main_queue(), ^{
               ........
               [self.delegate receivedGetData: parsedResponseData withActionCode: [NSNumber numberWithInt: DASHBOARD_REQUEST_DONE]];
           }
    }
}

我运行Profile Leaks工具,我在最后一行收到内存泄漏100%:

[self.delegate receivedGetData: parsedResponseData withActionCode: [NSNumber numberWithInt: DASHBOARD_REQUEST_DONE]];

我是iOS新手,我不知道为什么我这里有内存泄漏。谢谢你的帮助。

1 个答案:

答案 0 :(得分:-1)

你在街区内使用self。它会导致保留周期。需要使用对__weak的{​​{1}}引用来打破保留周期。

试试这个,

self

希望有所帮助!

相关问题