没有ARC泄漏的GCD /块

时间:2012-10-02 18:39:21

标签: objective-c ios memory-management objective-c-blocks grand-central-dispatch

我在以下使用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是一个自动释放的对象,如上所示。

1 个答案:

答案 0 :(得分:7)

newImages未自动释放。 new会返回一个拥有的引用 - 它等同于[[SomeClass alloc] init]