UIGraphicsBeginPDFPage() randomly crashes on 64bit devices (CGPDFSecurityManagerCreateDecryptor ())

狂风中的少年 提交于 2019-12-05 10:39:35

I had a crash on the CGPDFSecurityManagerCreateDecryptor method on 64-devices only with the following code:

CGPDFDocumentRelease(pdf);
CGDataProviderRelease(provider);
UIGraphicsEndPDFContext();

CGPDFSecurityManagerCreateDecryptor would get called when ending the context. The crash went away when I ended the context BEFORE releasing the document and provider.

UIGraphicsEndPDFContext();
CGPDFDocumentRelease(pdf);
CGDataProviderRelease(provider);

I've been struggling with this same issue too, and while Bill's answer gave me the clue I had to do it a little bit differently. In my situation there are a variable number of source PDFs that get copied into the target PDF so I can't just simply move UIGraphicsEndContext before CGPDFDocumentRelease. The code structure looks roughly like this:

UIGraphicsBeginPDFContextToFile(...);
// ...
for each attachment pdf {
    srcPdf = CGPDFDocumentCreateWithURL(...); // open source PDF
    // ...
    UIGraphicsBeginPDFPageWithInfo(...); // new page in target PDF, this randomly crashes
    // ...
    CGPDFDocumentRelease(srcPdf); // close source PDF
}
// ...
UIGraphicsEndPDFContext();

So instead I tried capturing the references to all the source PDFs it used and releasing them all after the rest of the target PDF is done, much later in the code. This is a kind of ugly because it moves the responsibility far away and holds all the memory until the end instead of releasing after each one is rendered... BUT it does appear to work! It's hard to say definitively since it's been a random crash but I haven't seen it since and I've been hammering it a lot trying to get it reoccur.

pdfRefs = [[NSPointerArray alloc] init];
UIGraphicsBeginPDFContextToFile(...);
// ...
for each attachment pdf {
    srcPdf = CGPDFDocumentCreateWithURL(...); // open source PDF
    // ...
    UIGraphicsBeginPDFPageWithInfo(...); // new page in target PDF, this randomly crashes
    // ...
    [pdfRefs addPointer:srcPdf]; // store for later closing
}
// ...
UIGraphicsEndPDFContext();
for each srcPdf in pdfRefs {
    CGPDFDocumentRelease(srcPdf); // close it here
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!