We are experiencing an issue where UIDocumentPickerViewController causes an uncaught exception when it is dismissed under certain conditions. The error appears as:
<NSXPCConnection: 0x301ac7660> connection from pid 2075 on anonymousListener or serviceListener: Warning: Exception caught during invocation of selector _updateRemoteBarButtonFrames:forUUID:, dropping incoming message and invalidating the connection.
Exception: DocumentManager service tried to send a message to a deallocated host proxy
DocumentManager service tried to send a message to a deallocated host proxy
(
0 CoreFoundation 0x0000000186c2a608 0013A8B1-2524-3534-B5BA-681AAF18C798 + 185864
1 libobjc.A.dylib 0x00000001841a5244 objc_exception_throw + 88
2 Foundation 0x000000018602eec0 E2F95328-659E-3C01-97F7-52B5B3BB7AA5 + 8576704
3 DocumentManager 0x00000002177cf7b8 0BAEFA1B-BD6D-3472-A1B3-6E09F5DE54F2 + 96184
4 CoreFoundation 0x0000000186c2d078 0013A8B1-2524-3534-B5BA-681AAF18C798 + 196728
5 CoreFoundation 0x0000000186c2cef0 _CF_forwarding_prep_0 + 96
6 Foundation 0x00000001858b8d8c E2F95328-659E-3C01-97F7-52B5B3BB7AA5 + 753036
7 Foundation 0x00000001858b7fc8 E2F95328-659E-3C01-97F7-52B5B3BB7AA5 + 749512
8 Foundation 0x00000001858b7220 E2F95328-659E-3C01-97F7-52B5B3BB7AA5 + 746016
9 Foundation 0x00000001858b70d8 E2F95328-659E-3C01-97F7-52B5B3BB7AA5 + 745688
10 libxpc.dylib 0x00000002119f1a50 527F7127-9586-32C8-9D8B-2972D39EAD7A + 72272
11 libxpc.dylib 0x00000002119f35cc 527F7127-9586-32C8-9D8B-2972D39EAD7A + 79308
12 libdispatch.dylib 0x000000010160a638 _dispatch_client_callout4 + 20
13 libdispatch.dylib 0x0000000101627eac _dispatch_mach_msg_invoke + 512
14 libdispatch.dylib 0x000000010161226c _dispatch_lane_serial_drain + 352
15 libdispatch.dylib 0x0000000101628ea4 _dispatch_mach_invoke + 492
16 libdispatch.dylib 0x000000010161226c _dispatch_lane_serial_drain + 352
17 libdispatch.dylib 0x0000000101613290 _dispatch_lane_invoke + 460
18 libdispatch.dylib 0x00000001016206fc _dispatch_root_queue_drain_deferred_wlh + 328
19 libdispatch.dylib 0x000000010161fd0c _dispatch_workloop_worker_thread + 580
20 libsystem_pthread.dylib 0x0000000211998680 _pthread_wqthread + 288
21 libsystem_pthread.dylib 0x0000000211996474 start_wqthread + 8
)
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'DocumentManager service tried to send a message to a deallocated host proxy'
Steps to Reproduce
- Present a UIDocumentPickerViewController inside a custom UIViewController.
- Dismiss the parent UIViewController before UIDocumentPickerViewController is fully visible.
- Swipe down or press the “Cancel” button in the picker.
- Occasionally, the system forcefully terminates the view service, causing a crash.
Workarounds Tried (Unsuccessful)
- Using UIAdaptivePresentationControllerDelegate – Not Called for UIDocumentPickerViewController.
- Using viewDidDisappear to clean up – Not Called when the system terminates the picker.
- Forcing dismissal before presenting a new picker
Reference to Line 42 in DOCRemoteViewController.m
From the error traceback, we see a reference to DOCRemoteViewController.m line 42, which strongly suggests that UIDocumentPickerViewController relies on a background process to manage its lifecycle. Since we don’t have access to Apple’s private code, we assume that the DocumentManager service is trying to send a message to an already deallocated instance of UIDocumentPickerViewController, leading to a crash.
One of the example wrapper implementation of us:
class DocumentPickerViewController: UIViewController, UIDocumentPickerDelegate {
private var picker: UIDocumentPickerViewController?
weak var delegate: UIDocumentPickerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
print("📄 DocumentPickerViewController - viewDidLoad() called")
setupDocumentPicker()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("📄 DocumentPickerViewController - viewDidAppear() called")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("📄 DocumentPickerViewController - viewWillAppear() called")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
print("📄 DocumentPickerViewController - viewWillDisappear() called")
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
print("📄 DocumentPickerViewController - viewDidDisappear() called")
}
deinit {
print("🗑 DocumentPickerViewController - deinit() called")
}
// MARK: - Setup Document Picker
private func setupDocumentPicker() {
picker = UIDocumentPickerViewController(forOpeningContentTypes: [.pdf])
picker?.delegate = self
picker?.allowsMultipleSelection = false
picker?.modalPresentationStyle = .formSheet
if let picker = picker {
present(picker, animated: false) {
print("📄 Document Picker fully presented")
}
}
}
// MARK: - UIDocumentPickerDelegate Methods
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
print("✅ User selected document(s): \(urls)")
delegate?.documentPicker?(controller, didPickDocumentsAt: urls)
dismissSelf()
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("❌ User cancelled document selection")
delegate?.documentPickerWasCancelled?(controller)
dismissSelf()
}
private func dismissSelf() {
DispatchQueue.main.async { [weak self] in
self?.dismiss(animated: true)
}
}
}
And logs for this implementation:
📄 Document Picker fully presented
📄 DocumentPickerViewController - viewDidAppear() called
❌ User cancelled document selection
The view service did terminate with error: Error Domain=_UIViewServiceErrorDomain Code=1 "(null)" UserInfo={Terminated=disconnect method}
📄 DocumentPickerViewController - viewDidLoad() called
📄 Document Picker fully presented
📄 DocumentPickerViewController - viewDidAppear() called
would appreciate any workarounds from the community to prevent crashes related to this issue!