简体   繁体   English

CGPDFDocumentCreateWithURL ARC内存泄漏

[英]CGPDFDocumentCreateWithURL ARC Memory Leak

I want to render PDFs on iOS and I'm getting a memory leak when I call CGPDFDocumentCreateWithURL() . 我想在iOS上渲染PDF,调用CGPDFDocumentCreateWithURL()时出现内存泄漏。 I know this issue has been discussed before, but I don't know whether my conditions are different because I'm using ARC. 我知道这个问题之前已经讨论过,但是我不知道我的情况是否有所不同,因为我使用的是ARC。

I've extracted and simplified the problem by running the following code on my viewDidLoad: 我通过在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. 仪器告诉我,泄漏对象是一个48字节的Malloc,由CoreGraphics负责。 And the stacktrace has CGPDFDocumentCreateWithURL in it. 并且stacktrace中包含CGPDFDocumentCreateWithURL。 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 我用问题的复制创建了一个git repo: https : //github.com/indika/PDFLeaks

Any ideas. 有任何想法吗。 Would appreciate any help or 'pointers'. 将不胜感激任何帮助或“指针”。

resourceRef is going to be overreleased. resourceRef将被过度释放。 Take a look at what you are doing. 看一下你在做什么。

1) Bridge resourceRef to ARC with no ownership change (still owned by ARC) 1)桥接resourceRef引用到ARC,而没有所有权更改(仍由ARC拥有)

2) CFRelease on resourceRef 2) CFRelease on resourceRef

3) ARC also releases resourceRef 3)ARC还发布resourceRef

You don't need step 2 您不需要步骤2

This is not a memory leak though, but an overrelease. 但是,这不是内存泄漏,而是过度释放。

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! 我正在加载损坏的PDF! I was using pdftk to slice and merge pdf documents, and I was breaking the indexes. 我正在使用pdftk切片和合并pdf文档,但我破坏了索引。 So remember, after merging a PDF with pdftk, repair the file with: 因此,请记住,将PDF与pdftk合并后,请使用以下方法修复文件:

pdftk original output destination

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

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