使用 NSCollectionView 时看到泄漏

Seeing leaks when using an NSCollectionView

我有一个简单的 NSCollectionView,它显示一组图像,每个图像下面都有一个复选框和一个标题。我已经 运行 使用了 Leaks 分析工具,它显示了一些泄漏。我试图了解泄漏的来源,因为我的代码 none 出现在泄漏的堆栈跟踪中。例如,仅在 collection 中显示一些项目,选择图像并关闭 window 会导致 15 个泄漏 NSMutableIndexSet objects。这些泄漏的大部分堆栈跟踪如下所示:

0 libsystem_malloc.dylib malloc_zone_calloc
1 libsystem_malloc.dylib calloc
2 libobjc.A.dylib class_createInstance
3 Foundation NSAllocateObject
4 AppKit -[NSCollectionViewLayoutAccessibility _visibleSections]
5 AppKit -[NSCollectionViewLayoutAccessibility _dumpVisibleChildren]
6 AppKit -[NSCollectionViewLayoutAccessibility accessibilityInvalidateLayout]
7 AppKit -[NSCollectionView layout]
8 AppKit -[NSView _doLayout]
9 AppKit -[NSView _layoutSubtreeWithOldSize:]
10 AppKit -[NSView _layoutSubtreeWithOldSize:]
11 AppKit -[NSView _layoutSubtreeWithOldSize:]
12 AppKit -[NSView _layoutSubtreeWithOldSize:]
13 AppKit -[NSView _layoutSubtreeWithOldSize:]
14 AppKit -[NSView _layoutSubtreeWithOldSize:]
15 AppKit -[NSView _layoutSubtreeWithOldSize:]
16 AppKit -[NSView _layoutSubtreeWithOldSize:]
17 AppKit -[NSView _layoutSubtreeWithOldSize:]
18 AppKit -[NSView layoutSubtreeIfNeeded]
19 AppKit -[NSWindow(NSConstraintBasedLayout) _layoutViewTree]
20 AppKit -[NSWindow(NSConstraintBasedLayout) layoutIfNeeded]
21 AppKit ___NSWindowGetDisplayCycleObserver_block_invoke6358
22 AppKit __37+[NSDisplayCycle currentDisplayCycle]_block_invoke
23 QuartzCore CA::Transaction::run_commit_handlers(CATransactionPhase)
24 QuartzCore CA::Context::commit_transaction(CA::Transaction*)
25 QuartzCore CA::Transaction::commit()
26 QuartzCore CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*)
27 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__
28 CoreFoundation __CFRunLoopDoObservers
29 CoreFoundation CFRunLoopRunSpecific
30 HIToolbox RunCurrentEventLoopInMode
31 HIToolbox ReceiveNextEventCommon
32 HIToolbox _BlockUntilNextEventMatchingListInModeWithFilter
33 AppKit _DPSNextEvent
34 AppKit -[NSApplication _nextEventMatchingEventMask:untilDate:inMode:dequeue:]
35 AppKit -[NSApplication run]
36 AppKit NSApplicationMain
37 libdyld.dylib start

我的 collection 查看数据源和委托代码非常简单。我不知道为什么这些会泄漏。任何人都可以帮助我了解这里发生了什么吗?

我的代码确实分配了一些 NSMutableIndexSet objects,但它们是自动释放的实例,因此不应该泄漏它们。 (而且,堆栈跟踪显示它不是来自我的代码创建它们的地方,所以它似乎不相关。)

如果有任何不同,我正在使用 ARC,在 macOS 10.11.6 (El Capitan) 上使用 Xcode 8.2.1 和 运行ning 构建我的应用程序。

一两个泄漏确实在堆栈跟踪中显示了我的代码的 2 行。基本上,对我的 window 控制器的 -loadWindow 方法的调用显示在堆栈的中间。当这些泄漏出现时,window 不会被释放,所以如果 window 中有任何东西保留在它们上面,它仍然在内存中(因此不是泄漏)。

根据 Apple 开发者论坛上的 this discussion,这似乎是 El Capitan 中已知的漏洞,已在 Sierra 中修复:

I'm no longer seeing the leak in when profiling in Instruments in Xcode 8 on Sierra. It looks likey did fix it actually. Thanks Apple