【发布时间】:2018-05-31 06:13:05
【问题描述】:
- 为了重现这个问题,只需创建一个 Cocoa 应用程序,选择 Swift 或 Objective-C(我都测试过)运行项目。
打开
debug navigator(你的应用的初始内存消耗 应该在 25MB 左右),只需使用你的空应用程序 下拉菜单,观察内存消耗在短时间内显着增长。然后从
Open Developer Tool在Xcode中打开Instruments,在Choose a profiling template选择Leaks并选择您的应用作为 目标,然后点击录制按钮。现在只需使用您的应用程序,调整窗口大小,打开下拉菜单 菜单。几分钟后你会注意到内存消耗会 超过 110MB,你会注意到泄漏点,如果你点击
您的应用程序的内存泄漏将继续增加,直到您的系统内存不足。
- 暂停然后点击检测到的泄漏点(时间线上的红色叉号),您可能会看到下面的一些罪魁祸首:
列表太长,我只列出了一些,但无一例外都是来自AppKit:
_NSLocalEventObserver 1 0x60000023ab00 32 Bytes AppKit + [NSEvent addLocalMonitorForEventsMatchingMask:placement:handler:]
_NSViewAuxiliary 1 0x110107080 448 Bytes AppKit -[NSView(NSInternal) _allocAuxiliary:]
NSPopUpButtonCell 1 0x6080001c7530 240 Bytes AppKit -[NSControl initWithFrame:]
NSControlAuxiliary 1 0x60800024c840 48 Bytes AppKit -[NSControl _commonEarlyInit]
NSMutableDictionary 1 0x60800024c6c0 48 Bytes AppKit -[NSCell setFont:]
NSPopUpButton 1 0x60800036e940 192 Bytes AppKit -[NSSpellChecker init]
NSCellAuxiliary 1 0x6080000eb280 128 Bytes AppKit -[NSCell init]
Malloc 48 Bytes 1 0x60800024c6f0 48 Bytes AppKit -[NSCell setFont:]
NSMenu 1 0x608000861f40 64 Bytes AppKit -[NSPopUpButtonCell initTextCell:pullsDown:]
_NSMenuImpl 1 0x6080000cdc10 112 Bytes AppKit -[NSMenu _createExtraIvars]
NSCarbonMenuImpl 1 0x6080000cdc80 112 Bytes AppKit -[NSMenu _createMenuImpl]
NSExtraMICData 1 0x608000038c00 32 Bytes AppKit -[NSMenuItemCell initTextCell:]
NSMutableArray 1 0x60800024daa0 48 Bytes AppKit -[NSMenu insertItem:atIndex:]
这是一个已知的错误吗? Xcode 生成的代码我没有接触过。 AppKit 或 Cocoa 框架中是否存在内存泄漏,或者两者都有?还是我做错了什么?
【问题讨论】:
标签: xcode macos memory-leaks