3

My app supports printing. If the user cancels the Printer Options modal view controller, the app crashes somewhere in Apple's code with a message of _WebTryThreadLock(bool) ... Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...

I've narrowed it down to the following code:

let itemProvider = UIActivityItemProvider(placeholderItem: "message")
let activityItems = [ itemProvider,
                      UIMarkupTextPrintFormatter(markupText: "test") ]
let activityController = UIActivityViewController(activityItems: activityItems,
                                              applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)

If I remove itemProvider from the activityItems array, the crash goes away. In my app I have a custom subclass of UIActivityItemProvider, but even if I use the superclass without customizing it, I get this crash.

To replicate, create a single-view project with a single button, and link the button to an action that uses the code snippet above as its body. Then tap the button, tap the Print icon in the activity view controller, and then tap the Cancel button in the Printer Options view controller. Crash. Any ideas?

Steve Liddle
  • 3,685
  • 3
  • 27
  • 39
  • Following your steps in Xcode 8.1, with an iPhone target, and running on a 9.3.5 device, I don't see a crash. – Phillip Mills Dec 06 '16 at 15:21
  • Thanks for checking this, Phillip. I'm using Xcode 8.1 with an iPhone 7 simulator, deployment target set to 10.1. When I change that to 9.3, I still get a crash in the simulator every time. I've tried on my device (iPhone 6 Plus running 10.1.1), and the crash doesn't always happen -- it's intermittent. If I set the deployment target to 10.1, my device does crash every time, but if I set the deployment target to 9.3 it doesn't always happen. In my shipping app, users are experiencing this crash on iPhones and iPads running 10.1.1. Perhaps it's an iOS bug and I need to file a Radar. – Steve Liddle Dec 06 '16 at 16:19
  • I have no good ideas but I did find one strange observation. If I assign a completion handler (`UIActivityViewControllerCompletionWithItemsHandler`) for the activity controller, it stops crashing in the simulator **the first time through the sequence**. But, if I repeat the UI actions without relaunching...crash. – Phillip Mills Dec 06 '16 at 19:23
  • I filed a radar, number 29578381. – Steve Liddle Dec 08 '16 at 18:01
  • I have the same issue. If I omit the first activity item (in my case a plain html string for emails), the issue disappears. Not a solution, but perhaps a pointer to the problem. Meanwhile I'll publish with just the print activity. – Reg Cappuccino Jan 12 '17 at 02:51

1 Answers1

5

It is almost certainly a bug in Apple's code.

After debugging, I came to the conclusion that it is related to deallocation of the UIMarkupTextPrintFormatter instance.

So as a workaround, you can have the formatter as a property in your class:

var printFormatter = UIMarkupTextPrintFormatter(markupText: "test")

Then use it like this:

let itemProvider = UIActivityItemProvider(placeholderItem: "message")
let activityItems = [itemProvider, self.printFormatter]
let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)

As long as the property is held strongly by your instance and is not deallocated, it won't crash.

Jonge
  • 221
  • 2
  • 8