UIImage imageWithData leak(GCD)

时间:2011-02-11 10:45:30

标签: cocoa-touch memory-leaks uiimage nsautoreleasepool grand-central-dispatch

您好我从Instrument获得了一个报告的漏洞,其中包含以下代码。

dispatch_async(requestQueue, ^{

        NSURL *urlRequest = [NSURL URLWithString:url];

        NSError *error = nil;
        NSData *imageData = [NSData dataWithContentsOfURL:urlRequest
                                                  options:0
                                                    error:&error];
        if (error != nil)
        {
            NSLog(@"%@ %@", [error localizedDescription], [error localizedFailureReason]);

            [[[[UIAlertView alloc] initWithTitle:[error localizedDescription]
                                         message:[error localizedFailureReason]
                                        delegate:nil
                               cancelButtonTitle:NSLocalizedString(@"OK", nil)
                               otherButtonTitles:nil] autorelease] show];
            return;
        }

        dispatch_async(dispatch_get_main_queue(), ^{

            image = [UIImage imageWithData:imageData];
            if (delegate != nil && [delegate respondsToSelector:@selector(imageRequestDone:)])              
            {
                [delegate imageRequestDone:image];
            }
        });
    }); 

泄漏报告我的图片= [UIImage imageWithData:imageData]泄漏,任何想法为什么? 我是否需要为自动释放的UIImage对象使用NSAutoreleasePool?

顺便说一下,如果它的问题,我宣布__block UIImage * image = nil;因为我在输入上面的代码之前检查了ImageCache中的图像。

1 个答案:

答案 0 :(得分:1)

您尚未释放队列:dispatch_release(requestQueue);