在后台线程上取消分配视图:如何调试?

时间:2016-11-28 12:10:42

标签: objective-c macos

有时我的应用程序崩溃是因为视图在后台线程上被释放,但问题是我无法弄清楚它来自哪里。

以下是崩溃日志:

2016-11-28 12:21:44.753913 iCompta[3993:326592] -[NSView _registerWithBackdropView] should not be called off of the main thread. Invocation was here:(
0   AppKit                              0x00007fff8f082779 -[NSView(NSInternal) _unregisterWithBackdropViewIfNeeded] + 404
1   AppKit                              0x00007fff8f0995b6 -[NSView(NSInternal) _recursivelyNoteBackdropViewChanged] + 54
2   AppKit                              0x00007fff8f099685 -[NSView(NSInternal) _recursivelyNoteBackdropViewChanged] + 261
3   AppKit                              0x00007fff8f099685 -[NSView(NSInternal) _recursivelyNoteBackdropViewChanged] + 261
4   AppKit                              0x00007fff8f099685 -[NSView(NSInternal) _recursivelyNoteBackdropViewChanged] + 261
5   AppKit                              0x00007fff8f159fdd -[NSTableView _removeBackgroundView] + 194
6   AppKit                              0x00007fff8f159f05 -[NSTableView viewWillMoveToSuperview:] + 65
7   AppKit                              0x00007fff8f03b4cc -[NSView _setSuperview:] + 695
8   AppKit                              0x00007fff8f1fddab -[NSTableView _setSuperview:] + 263
9   AppKit                              0x00007fff8f041205 -[NSView removeFromSuperview] + 447
10  AppKit                              0x00007fff8f3be133 -[NSOutlineView removeFromSuperview] + 56
11  AppKit                              0x00007fff8f1627ec -[NSView removeFromSuperviewWithoutNeedingDisplay] + 38
12  AppKit                              0x00007fff8f8d333e -[NSView _finalize] + 1008
13  AppKit                              0x00007fff8f04ece3 -[NSView dealloc] + 350
14  AppKit                              0x00007fff8f04eaf2 -[NSClipView dealloc] + 165
15  libobjc.A.dylib                     0x00007fffa5e47e8d _ZN12_GLOBAL__N_119AutoreleasePoolPage3popEPv + 781
16  Foundation                          0x00007fff92fb3b9b -[_NSXPCConnectionExportedObjectTable releaseExportedObject:] + 350
17  libdispatch.dylib                   0x0000000100c5d74d _dispatch_call_block_and_release + 12
18  libdispatch.dylib                   0x0000000100c53fcc _dispatch_client_callout + 8
19  libdispatch.dylib                   0x0000000100c6bb0b _dispatch_queue_serial_drain + 205
20  libdispatch.dylib                   0x0000000100c5d4f1 _dispatch_queue_invoke + 1174
21  libdispatch.dylib                   0x0000000100c56247 _dispatch_root_queue_drain + 671
22  libdispatch.dylib                   0x0000000100c55f58 _dispatch_worker_thread3 + 114
23  libsystem_pthread.dylib             0x0000000100ccb8c2 _pthread_wqthread + 1299
24  libsystem_pthread.dylib             0x0000000100ccb39d start_wqthread + 13
)

崩溃线程的名称是NSFileCoordinate,所以它似乎与文件协调有关,但这对我没有多大帮助。有没有想过调试这个?

1 个答案:

答案 0 :(得分:1)

最后,我经过多次调试后才能解决问题。在我的情况下,崩溃原点的视图是文档窗口的一部分,当你关闭文档时有时会发生崩溃。

当您关闭文档并进行修改时,会启动异步保存,我发现有时文档最终会在后台队列中释放,然后导向窗口,其中的视图也会在该后台队列中释放

所以我的解决方案是在我的文档的dealloc方法中对我的窗口的根视图进行强引用,并在1秒后在主队列上调度一个块以使该引用无效。这样我就可以确保在主线程上释放所有视图。