我在以下使用 GCD 的代码中有泄漏。注意:我没有使用 ARC。泄漏恰好在
__block NSMutableArray *newImages = [NSMutableArray new];
dispatch_async(serialQueue, ^{
for (NSDictionary *imageData in results) {
NSURL *url = [NSURL URLWithString:[imageData objectForKey:@"url"]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSError *error = nil;
NSHTTPURLResponse *response = nil;
NSData *imageData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error == nil && imageData != nil && response.statusCode == 200) {
UIImage *image = [UIImage imageWithData:imageData];
[newImages addObject:image];
}
else {
self.errorLabel.text = @"An error has occured downloading some images.";
[self.spinner stopAnimating];
}
}
});
根据仪器,我在以下位置泄漏:[newImages addObject:image];
。为什么会这样泄漏?newImages
是一个自动释放的对象,如上所示。