My app is randomly crashing on CGContext.drawPDFPage without a way to reproduce the bug consistently. It happens mostly on iPads. The code looks something like this:
override func draw(_ layer: CALayer, in ctx: CGContext) {
UIGraphicsPushContext(ctx)
ctx.saveGState()
defer {
ctx.restoreGState()
UIGraphicsPopContext()
}
ctx.setFillColor(UIColor.white.cgColor)
ctx.fill(viewBounds)
ctx.translateBy(x: 0, y: viewBounds.height)
ctx.scaleBy(x: 1, y: -1)
ctx.scaleBy(x: pdfScale, y: pdfScale)
ctx.interpolationQuality = .high
ctx.setRenderingIntent(.defaultIntent)
ctx.beginPDFPage(nil)
ctx.drawPDFPage(pdfPage) // This is where the crash happens
ctx.endPDFPage()
}
The stack trace ends in CGPDFObjectGetReference
and looks like this:
The error is: EXC_BAD_ACCESS
. If you see anything that could be causing this crash would help me a lot!
Anyone, who reached the similar crash, the case was in the cache layer which by index were requested CGPDFDocument
and CGPDFPage
in specific cases.
Solution is to request PDF from persistent storage on demand.
Starting from iOS 13 – we don't see any bugs related to this, thus probably Apple resolved the bug with restoring the context from values of CGPDFDocument
and CGPDFPage
types.