I created a demo Xcode project, it can reproduce the crash easily.
Please follow the 'STEPS TO REPRODUCE THE CRASH' below:
- Archive the demo project with Release configuration and Xcode15.3 or later versions
- Export ipa with adhoc.
- Install it on your real devices(iPhone or iPad).
- Launch the app.
- Press 'Parse PDF' button, then App will crash.
It’s likely that you’ll receive an official reply via your bug (FB14806158
) soon, but I want to expand on that a little. You wrote:
I created a
Thanks for that.
Note I wasn’t able to download your project via that link, but I was able to get it from your bug report. In general, if you’re sharing a test project here, it’s best to put it on a site, like GitHub, that doesn’t required authentication. For more hints and tips, see Creating a test project.
Looking at your project, I see that it has code like this:
var contents = String()
…
let stream = CGPDFContentStreamCreateWithPage(page)
let scanner = CGPDFScannerCreate(stream, operators, &contents)
// ^ Forming 'UnsafeMutableRawPointer'
// to an inout variable of type
// String exposes the internal
// representation rather than
// the string contents.
This is almost certainly the cause of the crash you’re seeing. Swift’s &
sigil has a very different meaning from the equivalent in C. I discuss this in gory detail in The Peril of the Ampersand.
A minimal fix for this would like:
var contents = String()
withUnsafeMutablePointer(to: &contents) { stringPointer in
let stream = CGPDFContentStreamCreateWithPage(page)
let scanner = CGPDFScannerCreate(stream, operators, stringPointer)
…
}
Your operator callbacks can now cast their context
parameter to an UnsafeMutablePointer<String>
, and modify the string as necessary.
However, my preferred option for dealing with this is to create a temporary object and then pass in self
. I have posted numerous examples of this in the past; here’s one that’s kinda similar to what you’re trying to do.
The nice thing about this approach is that it’s not limited to just one value. You can access an arbitrary amount of state simply by adding more properties to the class.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"