CGContextDrawImage - > ImageIO_Malloc泄漏

时间:2014-03-07 15:05:26

标签: ios memory-leaks cgcontextdrawimage

我花了很多时间试图弄清楚为什么我的应用程序中出现了ImageIO泄漏...如果有人可以帮我解决这个问题,我将不胜感激!

我有一个UIPageViewController,包含许多UIViewControllers,图像为2048x1536。我将UIImage直接设置为view.layer.contents是最佳的。当用户在图像之间滑动时,我懒洋洋地将它们加载到bg线程上然后缓存它们。我有一个最大缓存大小,所以在某些时候,应用程序开始卸载并重新加载图像,因为用户继续刷卡。一切正常但如果我开始快速滑动2或3个周期,我最终在调用CGContextDrawImage时会在ImageIO内部出现内存泄漏。泄漏仪器内部未报告泄漏。我真的不明白为什么会这样。这是我的代码:

...
Some code here check if there is already a running operation to load imagePath
If so just add the completion block to the completion block list and return

 NSBlockOperation* loadImageOperation = [[NSBlockOperation alloc] init];

// Make a weak reference to avoid a retain cycle
__weak NSBlockOperation* weakOp = loadImageOperation;

[loadImageOperation addExecutionBlock: ^{

    NSLog(@"Loading img %@",[imagePath lastPathComponent]);

    // So this can be executed concurrently several times if the user swipes fast
    UIImage* image = [self loadImageFileForCoreAnimation:imagePath];

    dispatch_async(dispatch_get_main_queue(), ^{

        if (image)
        {
            [self cacheImage:image withKey:imagePath];

            if (completion)
                completion(image);
        }
        else if (!weakOp.isCancelled)
        {
            NSLog(@"Error loading image %@", imagePath);
        }

        [self.runningOperationObjects removeObjectForKey:imagePath];
    });
}];

...

- (UIImage*)loadImageFileForCoreAnimation:(NSString*)imageFile

{     __weak NSOperation * weakRunningOp = [self getRunningOperationWithImagePath:imageFile];

CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename([imageFile UTF8String]);

CGImageRef image = CGImageCreateWithPNGDataProvider(dataProvider, NULL, NO, kCGRenderingIntentDefault);

if(!image)
    image = CGImageCreateWithJPEGDataProvider(dataProvider, NULL, NO, kCGRenderingIntentDefault);

CGDataProviderRelease(dataProvider);

if (weakRunningOp.isCancelled)
{
    CGImageRelease(image);
    return nil;
}

// Here we define the ARGB pixel format
CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little;
size_t width = CGImageGetWidth(image);
size_t height = CGImageGetHeight(image);
size_t bytesPerPixel = 4;
size_t bitsPerComponent = 8;
// bytesPerRow must be at least (width * bytesPerPixel) bytes;
size_t bytesPerRow = width * bytesPerPixel;
// Also : bytesPerRow must be an integer multiple of bytesPerPixel
bytesPerRow = ((bytesPerRow + (bytesPerPixel - 1)) / bytesPerPixel) * bytesPerPixel;

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef imageContext = CGBitmapContextCreate(NULL, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo);
  CGColorSpaceRelease(colorSpace);

if (weakRunningOp.isCancelled)
{
    CGImageRelease(image);
    CGContextRelease(imageContext);
    return nil;
}

// *** This function sometimes leaks!
CGContextDrawImage(imageContext, CGRectMake(0, 0, width, height), image);

CGImageRelease(image);

if (weakRunningOp.isCancelled)
{
    CGContextRelease(imageContext);
    return nil;
}

CGImageRef outputImage = CGBitmapContextCreateImage(imageContext);

UIImage* bitmapImage = [UIImage imageWithCGImage:outputImage scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];

CGImageRelease(outputImage);
CGContextRelease(imageContext);

return bitmapImage;

}

这是我的仪器截图,在某些时候,VM:ImageIO_PNG_Data以12mo分配出现,永不消失。

enter image description here

enter image description here

0 个答案:

没有答案