iOS leak instrument CGContextDrawPDFPage

1.6k views Asked by At

I know this question has been asked several times, but I couldn't solve it for my particular case. CGContextDrawPDFPage is indicated as a leak in the leak instrument. Also when this segment of code is run the app crashes which I'm really sure is due to memory issues.

    pdfURLDocument = [[NSURL alloc] initFileURLWithPath: [docsDir stringByAppendingPathComponent:documentName]];
    pdfDocument = CGPDFDocumentCreateWithURL((CFURLRef)pdfURLDocument);
    [pdfURLDocument release];

    page = CGPDFDocumentGetPage(pdfDocument, 1);
    CGPDFPageRetain(page);

    // determine the size of the PDF page
    CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
    pdfScaleWidth = (1/((CGFloat) gridSizeDocument)) * self.frame.size.width/pageRect.size.width;
    pdfScaleHeight = (1/((CGFloat) gridSizeDocument)) * self.frame.size.height/pageRect.size.height;
    pageRect.size = CGSizeMake(pageRect.size.width*pdfScaleWidth, pageRect.size.height*pdfScaleHeight);


    // Create a low res image representation of the PDF page        
    UIGraphicsBeginImageContext(pageRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // First fill the background with white.
    CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
    CGContextFillRect(context,pageRect);

    CGContextSaveGState(context);
    // Flip the context so that the PDF page is rendered
    // right side up.
    CGContextTranslateCTM(context, 0.0, pageRect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // Scale the context so that the PDF page is rendered 
    // at the correct size for the zoom level.
    CGContextScaleCTM(context, pdfScaleWidth, pdfScaleHeight);
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
    CGContextSetRenderingIntent(context, kCGRenderingIntentDefault); 
    CGContextDrawPDFPage(context, page);
    CGContextRestoreGState(context);

    backgroundImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    CGPDFPageRelease(page);

Also, I included CGPDFPageRelease(page); in the dealloc method. Also, it might be helpful to add that it works fine for small documents, but only crashes on large ones. Memory leaks still persist in the smaller ones, however.

2

There are 2 answers

0
Rob On

I know this is an old question, but two observations:

  1. You need to release your pdfDocument:

    CGPDFDocumentRelease(pdfDocument);
    
  2. You should not release the page with CGPDFPageRelease(page), though, because that is an autoreleased object and you don't own it (unless, of course, you retained it with CGPDFPageRetain).

If you use the static analyzer ("Analyze" on Xcode's "Product" menu), it should point out both of those issues.

The fundamental problem is that CGContextDrawPDFPage leaks in iOS versions prior to 6.0.

1
Felixyz On

The release needs to come after the page has been used, not before. So first, move CGPDFPageRelease(page) to last in this code block and see if that helps. Also, the problem could have something to do with the CGPDFDocumentRef stored in the pdf variable. If the above doesn't help, it would be good if you show how you obtain the reference, and where you retain and release it.