简体   繁体   English

iOS泄漏工具CGContextDrawPDFPage

[英]iOS leak instrument CGContextDrawPDFPage

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. CGContextDrawPDFPage在泄漏仪器中指示为泄漏。 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); 另外,我包括了CGPDFPageRelease(page); in the dealloc method. 在dealloc方法中。 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. 但是,较小的内存泄漏仍然存在。

I know this is an old question, but two observations: 我知道这是一个古老的问题,但有两个发现:

  1. You need to release your pdfDocument : 您需要释放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 ). 应该释放pageCGPDFPageRelease(page) ,虽然,因为这是一个自动释放的对象,你不拥有它(除非,当然,你保留它CGPDFPageRetain )。

If you use the static analyzer ("Analyze" on Xcode's "Product" menu), it should point out both of those issues. 如果使用静态分析器(Xcode的“产品”菜单上的“分析”),则应指出这两个问题。

The fundamental problem is that CGContextDrawPDFPage leaks in iOS versions prior to 6.0. 根本问题是CGContextDrawPDFPage在6.0之前的iOS版本中泄漏。

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. 因此,首先,将CGPDFPageRelease(page)移动到此代码块的最后,看看是否有帮助。 Also, the problem could have something to do with the CGPDFDocumentRef stored in the pdf variable. 此外,该问题可能有事情做与CGPDFDocumentRef存储在pdf的变量。 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. 如果上述方法没有帮助,那么请说明如何获得引用以及在何处保留和释放该引用将是一个很好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM