UIGraphicsBeginPDFPage () accidentally crashes on 64-bit devices (CGPDFSecurityManagerCreateDecryptor ())

I am struggling with the pdf export method, which worked very well until I ported the application to arm64 architecture.

Bacisally, the method opens an existing PDF file, it creates a new pdf file and draws the contents of the first pdf file into the newly created one before adding additional content pages.

When the method tries to create a new pdf page in the document (after the first PDF has been integrated into the new pdf), the application crashes with the warning EXC_BAD_ACCESS in UIGraphicsBeginPDFPage (); call.

This happens only with some PDF files, not all and only on 64-bit devices.

Here's a stacktrace that shows a call to CGPDFSecurityManagerCreateDecryptor (), which I could not find what it was doing.

Thread 14Queue : NSOperationQueue 0x14f6dd3a0 :: NSOperation 0x17504a470 (serial) #0 0x00000001838aeee4 in CGPDFSecurityManagerCreateDecryptor () #1 0x00000001838d1004 in pdf_filter_chain_create () #2 0x0000000183831e00 in CGPDFStreamCreateFilterChain () #3 0x000000018383226c in chain_get_bytes () #4 0x0000000183b5e0ac in unpackImageRow () #5 0x0000000183b5dfd4 in PDFImageEmitData () #6 0x0000000183b5f684 in emit_image () #7 0x0000000183b5ef9c in PDFImageEmitDefinition () #8 0x0000000183464584 in __CFSetApplyFunction_block_invoke () #9 0x00000001834643bc in CFBasicHashApply () #10 0x00000001834642e4 in CFSetApplyFunction () #11 0x0000000183b5fa9c in PDFImageSetEmitDefinitions () #12 0x0000000183b590c0 in emit_page_resources(PDFDocument*) () #13 0x0000000183b5904c in PDFDocumentEndPage () #14 0x0000000183b57cf0 in pdf_EndPage () #15 0x0000000187fda904 in UIGraphicsBeginPDFPageWithInfo () #16 0x00000001002093e8 in -[ExportTools renderPdfContentToContext:forPlanVersion:] #17 0x00000001001fba60 in -[ExportTools generatePdfReportWithOptions:] #18 0x00000001000f7eb4 in -[DetailViewController generatePdfAndShowModalOpenWithAppWithOptions:] #19 0x00000001835883c0 in __invoking___ () #20 0x0000000183486138 in -[NSInvocation invoke] () #21 0x000000018443ba20 in -[NSInvocationOperation main] () #22 0x000000018437c61c in -[__NSOperationInternal _start:] () #23 0x000000018443e26c in __NSOQSchedule_f () #24 0x000000010105cdf0 in _dispatch_client_callout () #25 0x0000000101067854 in _dispatch_queue_drain () #26 0x0000000101060120 in _dispatch_queue_invoke () #27 0x000000010106975c in _dispatch_root_queue_drain () #28 0x000000010106af18 in _dispatch_worker_thread3 () #29 0x00000001945012e4 in _pthread_wqthread () 

If you have any idea about this failure, your help will be greatly appreciated for the fact that one day you are trying to fix it and beg, if this is not a UIKit error ...

thanks

+5
source share
2 answers

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

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

CGPDFSecurityManagerCreateDecryptor will be called when the context ends. The wreck went away when I finished the context before releasing the document and the provider.

 UIGraphicsEndPDFContext(); CGPDFDocumentRelease(pdf); CGDataProviderRelease(provider); 
+1
source

I am also struggling with this problem, and while Bill's answer gave me the key, I had to do it differently. In my situation, there is a variable number of source PDF files that are copied to the destination PDF, so I just can't just move the UIGraphicsEndContext to CGPDFDocumentRelease . The code structure looks something 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 to grab the links to all the source PDF files that he used and free them all after completing the remaining destination PDF file, much later in the code. This is kind of ugly, because it gives responsibility away and holds all the memory to the end, and is not freed after each of them is displayed ... BUT it really works! It is hard to say definitively, since it was an accidental failure, but since then I have not seen it, and I have scored it a lot, trying to repeat it.

 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 } 
0
source

Source: https://habr.com/ru/post/1209573/


All Articles