3

i spent so much time trying to figure out why i get ImageIO leaks in my app...if someone could help me on this one it would be appreciated!

I have a UIPageViewController, containing a lot of UIViewControllers with images of 2048x1536. I set the UIImage directly to the view.layer.contents to be optimal. As the user swipe between images, i lazily load them on a bg thread and then cache them. I have a maximum cache size so at some point the app starts to unload and reload images as the user continue to swipe. All works fine but if i start to swipe really fast for 2 or 3 cycles, i eventually get memory leak inside ImageIO when calling CGContextDrawImage. The leak is not reported inside the Leak instrument. I dont really understand why this is happening. Here is my code :

...
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;

}

Here is my instrument screenshot, at some point, VM : ImageIO_PNG_Data appears with a 12mo allocation and never disappear.

enter image description here

enter image description here

AkademiksQc
  • 677
  • 1
  • 6
  • 20

0 Answers0