Drawing a PDF page into another PDF results in a crash.

Originator:ZTZach
Number:rdar://10481606 Date Originated:22-Nov-2011 02:14 PM
Status:Open Resolved:
Product:iPad SDK Product Version:5.0.1
Classification: Reproducible:Always
 
22-Nov-2011 02:14 PM Zachary Starkebaum:
Summary:
In iOS 5 when drawing a PDF page into a new PDF context, there is a crash when trying to close the new context. 

Steps to Reproduce:
With a culprit PDF, create a new PDF context and try to draw a page of the PDF into the new context.

Expected Results:
Normally this would result in a new PDF with that same page as the original PDF.

Actual Results:
Exc_bad_access crash and a corrupted PDF output document.

Regression:
This occurs in iOS 5.0.1 but not in iOS 4

Notes:
The backtrace seems to indicate this issue is related to embedding a font from the original PDF into the new PDF context.

Comments

Works 99% of the time

In fact, only one document of ours causes this error out of the hundreds we've tried, and it's only on certain pages within that document. My company would be happy to release this document to you if it will help resolve the issue.

In this method, we are making a copy of one page of the source document. I've added comments to show the relevant lines.

  • (CGPDFDocumentRef)newSinglePageDocumentFromDocument:(CGPDFDocumentRef)document page:(NSInteger)pageNumber { CGPDFDocumentRef sourceDocument = CGPDFDocumentRetain(document); CGPDFDocumentRef newDocument = NULL;

    CFMutableDataRef consumerData = CFDataCreateMutable(kCFAllocatorDefault, 0); CGDataConsumerRef contextConsumer = CGDataConsumerCreateWithCFData(consumerData);

    CGPDFPageRef page = CGPDFDocumentGetPage(sourceDocument, pageNumber); const CGRect mediaBox = CGPDFPageGetBoxRect(page, kCGPDFCropBox); CGContextRef ctx = CGPDFContextCreate(contextConsumer, &mediaBox, NULL);

    if (ctx) { if (page) { CGPDFContextBeginPage(ctx, NULL); CGContextDrawPDFPage(ctx, page); //Commenting out this line prevents the exception CGPDFContextEndPage(ctx); }

    CGPDFContextClose(ctx);  //EXC_BAD_ACCESS thrown here
    CGContextRelease(ctx);
    
    CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)consumerData);
    newDocument = CGPDFDocumentCreateWithProvider(provider);
    CGDataProviderRelease(provider);
    

    } CGDataConsumerRelease(contextConsumer); CFRelease(consumerData); CGPDFDocumentRelease(sourceDocument);

    return newDocument; }


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!