2011-03-08 115 views
5

你好,這裏是我在CATiledlayer繪製PDF的代碼CGContextDrawPDFPage內存泄露

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 
{ 

     CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0); 
     CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx)); 
     CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height); 
     CGContextScaleCTM(ctx, 1.0, -1.0); 
     CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFCropBox, layer.bounds, 0, true)); 
     CGContextDrawPDFPage(ctx, myPageRef); 
} 

一切都很好,但我在下面一行

 CGContextDrawPDFPage(ctx, myPageRef); 

了內存泄漏警告這裏myPageRef是CGPDFPageRef

回答

5

我已經從github上下載了代碼,並做了一些R & D,並發現,

我忘了釋放CGPDFPageRelease(myPageRef)我TiledView的dealloc方法..

和編寫這些代碼我的內存泄漏解決後....

// Clean up. 

- (void)dealloc { 
    CGPDFPageRelease(myPageRef); 
    [super dealloc]; 
} 
+0

你應該只能做,如果你保留它在某個時候。但是如果你只是在做'CGPDFDocumentGetPage',你會得到一個autorelease對象,因此你不應該釋放它。 (顯然,如果你保留它,那麼當然你必須按照這個答案中的建議釋放它)。 – Rob 2013-07-17 14:07:15

+0

https://stackoverflow.com/questions/46903182/cgcontextdrawpdfpage-memory-leak-app-crash – Ravindhiran 2017-10-24 06:26:49