UIImage imageWithData 泄漏(GCD)
您好,我收到来自 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];
}
});
});
Leaks 报告我的 image = [UIImage imageWithData:imageData] 泄漏,有什么想法吗? 我是否需要 NSAutoreleasePool 来自动释放 UIImage 对象?
顺便说一句,如果有问题的话,我声明 __block UIImage *image = nil;上面是因为我在输入上面的代码之前检查了 ImageCache 中的图像。
Hi I am getting a reported leak from Instrument with the following code.
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];
}
});
});
Leaks reports that my image = [UIImage imageWithData:imageData] leaks, any ideas why?
Do I need to a NSAutoreleasePool for the autoreleased UIImage object?
Btw, If its matter, I declared __block UIImage *image = nil; above because I check for the image in an ImageCache before entering the code above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尚未释放队列:
dispatch_release(requestQueue);
You haven't released your queue:
dispatch_release(requestQueue);