【问题标题】:Is there a memory leak in Cocoa framework? Or am I missing something?Cocoa 框架中是否存在内存泄漏?还是我错过了什么?
【发布时间】:2018-05-31 06:13:05
【问题描述】:
  1. 为了重现这个问题,只需创建一个 Cocoa 应用程序,选择 Swift 或 Objective-C(我都测试过)运行项目。
  2. 打开debug navigator(你的应用的初始内存消耗 应该在 25MB 左右),只需使用你的空应用程序 下拉菜单,观察内存消耗在短时间内显着增长。

  3. 然后从Open Developer Tool在Xcode中打开Instruments,在 Choose a profiling template 选择 Leaks 并选择您的应用作为 目标,然后点击录制按钮。

  4. 现在只需使用您的应用程序,调整窗口大小,打开下拉菜单 菜单。几分钟后你会注意到内存消耗会 超过 110MB,你会注意到泄漏点,如果你点击

  5. 您的应用程序的内存泄漏将继续增加,直到您的系统内存不足。

  6. 暂停然后点击检测到的泄漏点(时间线上的红色叉号),您可能会看到下面的一些罪魁祸首:

列表太长,我只列出了一些,但无一例外都是来自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


    【解决方案1】:

    由于 (4) 内存大小会增长——像菜单这样的对象在第一次使用之前可能不会被分配,等等——但是这种增长不会是无限的。

    无法重现您的结果:在 10.13.2 和 Xcode 9.2 下运行没有看到无限增长,也没有报告泄漏。

    这并不是说您没有看到它,只是原因未知,但不太可能是框架中的“泄漏”本身。尝试另一台机器,检查您的设置等。

    【讨论】:

    • 感谢您抽出宝贵时间实际尝试一下,看看是否真的有问题。
    • 在所有对象都被分配内存消耗后购买方式不应超过某个点,在这种情况下确实如此。
    猜你喜欢
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 2011-06-26
    • 2011-10-07
    相关资源
    最近更新 更多