UIGraphicsGetImageFromCurrentImageContext memory leak with previews

Alessandro picture Alessandro · Feb 25, 2011 · Viewed 27.7k times · Source

I'm trying to create previews images of pages in a PDF but I have some problems with the release of memory.

I wrote a simple test algorithm that cycles on the problem, the app crashes near the 40th iteration:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"myPdf.pdf"];
CFURLRef url = CFURLCreateWithFileSystemPath( NULL, (CFStringRef)pdfPath, kCFURLPOSIXPathStyle, NO );
CGPDFDocumentRef myPdf = CGPDFDocumentCreateWithURL( url );
CFRelease (url);
CGPDFPageRef page = CGPDFDocumentGetPage( myPdf, 1 );

int i=0;
while(i < 1000){

    UIGraphicsBeginImageContext(CGSizeMake(768,1024));
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
    CGContextFillRect(context,CGRectMake(0, 0, 768, 1024));
    CGContextSaveGState(context);
    CGContextTranslateCTM(context, 0.0, 1024);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawPDFPage(context, page);
    CGContextRestoreGState(context);

    // --------------------------
    // The problem is here (without this line the application doesn't crash)
    UIImageView *backgroundImageView1 = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
    // --------------------------

    UIGraphicsEndImageContext();
    [backgroundImageView1 release];

    NSLog(@"Loop: %d", i++);
}

CGPDFDocumentRelease(myPdf);

The above-mentioned line seems to generate a memory leak, however, instruments doesn't show memory problems;

Can I escape from this kind of mistake?someone can explain me in which way? Are there other ways to show previews of a pdf?

UPDATE

I think the problem isn't the release of UIImage created by the method UIGraphicsGetImageFromCurrentImageContext() but the release of UIImageView created with this autorelease image.

I have divided the line of code in three steps:

UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext();
UIImageView *myImageView = [[UIImageView alloc] init];
[myImageView setImage: myImage]; // Memory Leak

The first and second lines doesn't create memory leaks so I think that the method UIGraphicsGetImageFromCurrentImageContext is not the problem.

I also tried as follows but the problem persists:

UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage];

I think there is a memory leak in the release of a UIImageView that contains a UIImage with the autorelease property.

I tried to write my object UIImageView inheriting a UIView as explained in this thread.

This solution works but isn't very elegant, it's a workaround, I would prefer to use the object UIImageView solving the memory problem.

Answer

Ole Begemann picture Ole Begemann · Feb 25, 2011

The problem is this:

UIGraphicsGetImageFromCurrentImageContext()

returns an autoreleased UIImage. The autorelease pool holds on to this image until your code returns control to the runloop, which you do not do for a long time. To solve this problem, you would have to create and drain a fresh autorelease pool on every iteration (or every few iterations) of your while loop.