【问题标题】:NSOutlineView crash on Mac OS versions below 10.12 as 'stronglyReferencesItems' set to 'false' by defaultNSOutlineView 在低于 10.12 的 Mac OS 版本上崩溃,因为默认情况下“stronglyReferencesItems”设置为“false”
【发布时间】:2020-03-05 15:47:10
【问题描述】:

我的应用有一个大纲视图,可以从服务器获取频繁更新。每当我收到更新时,我都会重新加载大纲视图。我同时对轮廓进行多项操作,例如在鼠标悬停时显示一些按钮、展开/折叠项目。对于这些操作,我使用 NSOutlineView.item(atRow:) 从大纲视图中获取项目

问题是,在 10.12 以下的操作系统版本上,NSOutlineView 内部调用 NSTableViewdelegate 方法的区域中,我的应用程序在随机情况下因 EXC_BAD_ACCESS (SIGSEGV) 而崩溃。我知道NSOutlineView 对于操作系统版本 10.12 中的项目有自己的保留和释放周期,因为默认情况下 stronglyReferencesItems 设置为 true。所以崩溃不会发生在 10.12 以上。

那么我该如何解决这个问题呢?有人可以指导我如何在较低版本上手动保留和释放传递给NSOutlineView 的项目。

简单地说,我需要在 10.12 以下的版本中启用 stronglyReferencesItems 设置为 true 的行为。

注意:正如我上面所说,我使用NSOutlineView 执行复杂操作,例如移动事件、展开/折叠(在某些情况下展开所有项目)、重新加载列表并从服务器频繁更新。所以在这里提到我的代码会很复杂。

以下是供您参考的崩溃日志之一:

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libswiftCore.dylib              0x000000010c5cec51 swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1> >::incrementSlow(swift::RefCountBitsT<(swift::RefCountInlinedness)1>, unsigned int) + 33
1   libswiftCore.dylib              0x000000010c5ab842 _swift_retain_n_(swift::HeapObject*, unsigned int) + 66
2   libswiftCore.dylib              0x000000010c5f7ac8 swift_bridgeObjectRetain_n + 104
3   com.prosoftnet.remotepcSuite    0x000000010bc8c626 RPCHostGroupTableCellView.setupView(group:) (in RemotePCSuite) (RPCHostGroupTableCellView.swift:0)
4   com.prosoftnet.remotepcSuite    0x000000010bd61198 RPCHostListViewController.outlineView(_:viewFor:item:) (in RemotePCSuite) (RPCHostListViewController.swift:579)
5   com.prosoftnet.remotepcSuite    0x000000010bd61a10 @objc RPCHostListViewController.outlineView(_:viewFor:item:) (in RemotePCSuite) (<compiler-generated>:0)
6   com.apple.AppKit                0x00007fff8f19e0d0 -[NSTableView(NSTableViewViewBased) makeViewForTableColumn:row:] + 76
7   com.apple.AppKit                0x00007fff8f19d541 -[NSTableRowData _addViewToRowView:atColumn:row:] + 300
8   com.apple.AppKit                0x00007fff8f19d27a -[NSTableRowData _addViewsToRowView:atRow:] + 184
9   com.apple.AppKit                0x00007fff8f19b9ad -[NSTableRowData _initializeRowView:atRow:] + 373
10  com.apple.AppKit                0x00007fff8f19aad2 -[NSTableRowData _addRowViewForVisibleRow:withPriorView:] + 396
11  com.apple.AppKit                0x00007fff8f23a9d1 -[NSTableRowData _addRowViewForVisibleRow:withPriorRowIndex:inDictionary:withRowAnimation:] + 254
12  com.apple.AppKit                0x00007fff8f23a7a3 -[NSTableRowData _unsafeUpdateVisibleRowEntries] + 1856
13  com.apple.AppKit                0x00007fff8f239fc7 -[NSTableRowData updateVisibleRowViews] + 230
14  com.apple.AppKit                0x00007fff8f23fe82 -[NSTableView viewWillDraw] + 178
15  com.apple.AppKit                0x00007fff8f2bd83f -[NSOutlineView viewWillDraw] + 169
16  com.apple.AppKit                0x00007fff8f0e6fa1 -[NSView(NSInternal) _sendViewWillDrawAndRecurse:] + 535
17  com.apple.AppKit                0x00007fff8f058ae1 -[NSView(NSLayerKitGlue) _layoutSublayersOfLayer:] + 142
18  com.apple.QuartzCore            0x00007fff8dd79404 -[CALayer layoutSublayers] + 219
19  com.apple.AppKit                0x00007fff8f058a3c _NSBackingLayerLayoutSublayers + 158
20  com.apple.QuartzCore            0x00007fff8dd78fe8 CA::Layer::layout_if_needed(CA::Transaction*) + 366
21  com.apple.QuartzCore            0x00007fff8dd78e66 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 24
22  com.apple.QuartzCore            0x00007fff8dd78602 CA::Context::commit_transaction(CA::Transaction*) + 242
23  com.apple.QuartzCore            0x00007fff8dd7839e CA::Transaction::commit() + 390
24  com.apple.QuartzCore            0x00007fff8dd86f09 CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 71
25  com.apple.CoreFoundation        0x00007fff99bdaf47 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
26  com.apple.CoreFoundation        0x00007fff99bdaea0 __CFRunLoopDoObservers + 368
27  com.apple.CoreFoundation        0x00007fff99bcca18 CFRunLoopRunSpecific + 328
28  com.apple.HIToolbox             0x00007fff8eb4256f RunCurrentEventLoopInMode + 235
29  com.apple.HIToolbox             0x00007fff8eb422ea ReceiveNextEventCommon + 431
30  com.apple.HIToolbox             0x00007fff8eb4212b _BlockUntilNextEventMatchingListInModeWithFilter + 71
31  com.apple.AppKit                0x00007fff8f08a8ab _DPSNextEvent + 978
32  com.apple.AppKit                0x00007fff8f089e58 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 346
33  com.apple.AppKit                0x00007fff8f07faf3 -[NSApplication run] + 594
34  com.apple.AppKit                0x00007fff8effc244 NSApplicationMain + 1832
35  com.prosoftnet.remotepcSuite    0x000000010bc519a9 main (in RemotePCSuite) (AppDelegate.swift:12)
36  libdyld.dylib                   0x00007fff8e4b95c9 start + 1

【问题讨论】:

  • 有什么理由不能在运行时将 'stronglyReferencesItems' 设置为 true 吗?我相信它在 10.12 之前仍然存在
  • 我该怎么做?
  • NSOutlineView.stronglyReferenceItems = true developer.apple.com/documentation/appkit/nsoutlineview/…
  • 此属性仅从 10.12 开始可用。 Apple 文档中也提到了这一点。

标签: swift cocoa nsoutlineview manual-retain-release


【解决方案1】:

您可以在以下方法中第一次从表中引用项目时将它们保留在数组中

var items = [ItemType]()
func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) 
{
    let item = getItem()
    items.append(item)
    return item
}

你会遇到麻烦,不知道什么时候可以释放它们。您必须手动处理该逻辑

【讨论】:

  • 是的,这就是我现在正在做的事情。我们需要谨慎释放这些物品。如果您在重新加载时释放项目并且用户展开或折叠父项,这可能会导致崩溃。
猜你喜欢
  • 1970-01-01
  • 2012-08-09
  • 2020-08-24
  • 2013-08-16
  • 1970-01-01
  • 2021-12-06
  • 2022-01-10
  • 2012-03-11
  • 2013-03-03
相关资源
最近更新 更多