user373017
user373017

Reputation: 183

pdf object creating memory leak

I have following code

NSString *filePath=[[NSString alloc] initWithString:[[NSBundle mainBundle] pathForResource:pdfname ofType:@"pdf" inDirectory:@"appMasterPdf"]]; 
NSURL *url = [NSURL fileURLWithPath:filePath];
[filePath release];

CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)url);
CGFloat scaleRatio; 
UIGraphicsBeginImageContext(CGSizeMake(width, height));
CGContextRef context = UIGraphicsGetCurrentContext();
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, pageNumber);
pageRect = CGPDFPageGetBoxRect(page, kCGPDFBleedBox);
width=pageRect.size.width;
height=pageRect.size.height;
if(pageRect.size.width/pageRect.size.height < 1.0) {
    scaleRatio = height/pageRect.size.height;
}
else {
    scaleRatio = width/pageRect.size.width;
}

CGFloat xOffset = 0.0;
CGFloat yOffset = height;
if(pageRect.size.width*scaleRatio<width) {
    xOffset = (width/2)-(pageRect.size.width*scaleRatio/2);
}
else {
    yOffset = height-((height/2)-(pageRect.size.height*scaleRatio/2));
}

CGContextTranslateCTM(context, xOffset, yOffset);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSaveGState(context);
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page,
                                                              kCGPDFBleedBox, CGRectMake(0, 0, pageRect.size.width, pageRect.size.height),
                                                              0, true);
pdfTransform = CGAffineTransformScale(pdfTransform, scaleRatio, scaleRatio);
CGContextConcatCTM(context, pdfTransform);
CGContextDrawPDFPage(context, page);
UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext();
CGContextRestoreGState(context);
UIGraphicsEndPDFContext();
UIGraphicsEndImageContext();

CGContextRelease(context);
CGPDFPageRelease(page);
return tempImage;

This function is acalling in for loop

CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)url); These lines give me a memory leaks. Please help me . Thanx in advanced

Upvotes: 2

Views: 1864

Answers (2)

Danny Bravo
Danny Bravo

Reputation: 4658

You should not call CGPDFPageRelease(page) method since the page is obtained using a get method. instead, replace that call with taskinoor's CGPDFDocumentRelease(pdf) and you should be good to go.

Upvotes: 2

taskinoor
taskinoor

Reputation: 46027

From the manual of CGPDFDocumentCreateWithURL, "You are responsible for releasing the object using CGPDFDocumentRelease."

So you need to call

CGPDFDocumentRelease(pdf)
when you are done. The general convention of CG methods is that if the name contains Create then there will be a corresponding Release method which you must call.

Upvotes: 2

Related Questions