5

Hello here is my code for drawing pdf in CATiledlayer

- (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);
 }

All is well but i got memory leak warning in following line

     CGContextDrawPDFPage(ctx, myPageRef);

Here myPageRef is CGPDFPageRef

Mehul Mistri
  • 15,037
  • 14
  • 70
  • 94

2 Answers2

5

I had download the code from github and make some R&D and found that,

I forgot to release CGPDFPageRelease(myPageRef) in dealloc method of my TiledView..

and after writing this code my memory leak solved....

 // Clean up.

 - (void)dealloc {
     CGPDFPageRelease(myPageRef);   
     [super dealloc];
 }
Mehul Mistri
  • 15,037
  • 14
  • 70
  • 94
  • 1
    You should only do that if you retained it at some point. But if you're just doing `CGPDFDocumentGetPage`, you get an autorelease object and therefore you shouldn't release it. (Obviously, if you retained it, then of course you _must_ release it as suggested in this answer). – Rob Jul 17 '13 at 14:07
  • https://stackoverflow.com/questions/46903182/cgcontextdrawpdfpage-memory-leak-app-crash – Ravindhiran Oct 24 '17 at 06:26
3

Calling

CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);

before CGContextDrawPDFPage solved a similar problem of mine.

Credits goes to this answer of Johann: CGContextDrawPDFPage taking up large amounts of memory

Community
  • 1
  • 1
aslı
  • 8,740
  • 10
  • 59
  • 80