0
votes

I want to render PDFs on iOS and I'm getting a memory leak when I call CGPDFDocumentCreateWithURL(). I know this issue has been discussed before, but I don't know whether my conditions are different because I'm using ARC.

I've extracted and simplified the problem by running the following code on my viewDidLoad:

// Bridge resourceRef to ARC with no ownership change (still owned by ARC)
CFURLRef resourceRef = (__bridge CFURLRef) [NSURL fileURLWithPath:htmlPath];

CGPDFDocumentRef pdf;
pdf = CGPDFDocumentCreateWithURL(resourceRef);
CGPDFDocumentRelease(pdf);

// Do not need to release resourceRef because ARC will release it

Instruments tells me that the Leaked Object is a Malloc 48 Bytes, responsible by CoreGraphics. And the stacktrace has CGPDFDocumentCreateWithURL in it. Leaking means that there is object out there without an owner.

I've create a git repo with a replication of the problem: https://github.com/indika/PDFLeaks

Any ideas. Would appreciate any help or 'pointers'.

2
I might have found the reason behind the leak: if my PDF originates from the NSCachesDirectory directory, then the leak occurs. Perhaps I should use a different directory for downloadable content.indi
I might be wrong about the leak being dependant on the directory the pdf is locatedindi
I ran the project from your link but it shows no leaks...borrrden

2 Answers

1
votes

resourceRef is going to be overreleased. Take a look at what you are doing.

1) Bridge resourceRef to ARC with no ownership change (still owned by ARC)

2) CFRelease on resourceRef

3) ARC also releases resourceRef

You don't need step 2

This is not a memory leak though, but an overrelease.

0
votes

I made many mistakes, especially with double releasing the document reference. However, that wasn't the real critter in my situation.

I was loading a broken PDF! I was using pdftk to slice and merge pdf documents, and I was breaking the indexes. So remember, after merging a PDF with pdftk, repair the file with:

pdftk original output destination