处理按键事件时 NSTextField 泄漏
我是这个论坛的新手,我已经搜索过,但没有找到这个问题的任何答案,这个问题在本周的大部分时间里一直困扰着我。
每次按下按键时 NSTextField 都会导致内存泄漏。
我已将这个问题从我的代码中分离出来,并且可以按如下方式重现:
- 创建一个新的“Cocoa Application”项目。
- 在主菜单 xib 窗口中放置一个 NSTextfield。 (无约束、出口或操作)
- 产品 ->配置文件中,选择“内存泄漏”工具(在 XCode 4 中)
在用户在文本字段中输入字符之前不会泄漏。 我得到了 8 个泄露的 32 字节 NSCFString 对象,其类型如下:
Library Responsible Caller 0 CFString Malloc 00:11.524.538 1 0x100130bb0 32 AppKit -[NSEvent charactersIgnoringModifiers] 1 CFString Malloc 00:11.622.145 1 0x100136950 32 AppKit -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:]
请注意,该项目除了自动生成的内容之外没有任何代码。
如果其他人可以重现这个问题,甚至可能提供如何防止它发生的建议,我会很感兴趣。
OS X 10.6.8 和 XCode 4.0.2(3.2.6 也会出现同样的问题)
任何建议将不胜感激,并且会减少我当前的挫败感。
更新:
在不同的 Mac 上尝试了上述项目创建。 发现其中之一没有产生泄漏(使用与上面相同的步骤创建项目) 在从未安装过 XCode 的 Mac 上安装了 XCode 4 - 没有泄漏!
- 使用以下过程在相当新的 MBPro 上卸载 Xcode3.2.6: http://macdevelopertips.com /xcode/how-to-uninstall-xcode.html
- 重新安装 XCode4
仍然泄漏内存。
- 擦除了相同的硬盘MPro,安装了 OS X 10.6,软件更新到 10.6.8,
- 安装了 XCode4
- 创建了与上面相同的测试项目
没有内存泄漏!!!!!
现在很明显,这个问题的根源在于安装而不仅仅是仪器的“误报”。我的配置文件运行被执行了多次,因此存在一次性行为,并且该行为在我的 Mac 上 100% 可重现。
现在的问题:我仍然有一台 27 英寸 iMac,但无法选择硬盘擦除。 我猜测安装的某些东西(框架?)在升级或重新安装 XCode 时不会更新/删除。
非常感谢您对此问题原因的想法。
I'm new to this forum and I have searched but not found any answers to this problem which has been puzzling me for best part of this week.
An NSTextField causes a memory leak every time a key is pressed.
I have isolated this problem from my code and it can be reproduced as follows:
- Create a new "Cocoa Application" project.
- Place an NSTextfield in the main menu xib window. (No Binding, Outlet or Action)
- Product -> Profile, choose "memory leak" instrument (in XCode 4)
No leaks until the user enters a character in the text field.
I get 8 leaked NSCFString objects of 32bytes of the following types:
Library Responsible Caller 0 CFString Malloc 00:11.524.538 1 0x100130bb0 32 AppKit -[NSEvent charactersIgnoringModifiers] 1 CFString Malloc 00:11.622.145 1 0x100136950 32 AppKit -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:]
Please note that this project has no code other than the auto generated stuff.
I would be interested if anyone else can reproduce this problem and maybe even offer a suggestion how to prevent it from occuring.
OS X 10.6.8 with XCode 4.0.2 (same problem occurs with with 3.2.6)
Any suggestion would be greatly appreciated and would decrease my current level of frustration.
Update:
Tried the above project creation on different macs.
Found that one of them produces no leaks (using the same procedure as above to create project)
Installed XCode 4 on a mac that never had XCode installed - no leaks!
- Unistalled Xcode3.2.6 on fairly new MBPro using this procedure: http://macdevelopertips.com/xcode/how-to-uninstall-xcode.html
- Re-Installed XCode4
STILL LEAKING MEMORY.
- Erased the HDD on same MPro, installed OS X 10.6, software update to 10.6.8
- installed XCode4
- created same test project as above
NO MEMORY LEAKS!!!!!
It is now clear that the root of this problem is somewhere in the installation and not just a "False Positive" of Instruments. My profile runs were executed multiple times so there is being a one-off and the behaviour was 100% re-producable on my macs.
Problem now: I still have a 27" iMac where HDD erase is not an option.
I'm guessing that there is something installed (Framework?) that doesn't get updated/deleted when upgrading or reinstalling XCode.
Your ideas on the cause of this issue are greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这几乎肯定是误报。 Leaks 工具并非万无一失,它可能会将某些并非万无一失的事物标记为泄漏(单例是一个常见的示例),请参阅 我对这个问题的回答。
我怀疑这里看到的是字段编辑器的创建,它为每个包含至少一个文本字段单元格的窗口创建一次。它是惰性创建的,因此仅在需要时(即开始文本编辑时)才会实例化。然后它会被重新用于该窗口中的所有文本字段编辑,并且在窗口消失之前不会被释放。这正是可能在泄漏工具中触发误报的行为。
诸如
NSTextField
之类的常见对象极不太可能出现重大内存泄漏。它们已经过非常彻底的测试,并且在操作系统版本之间没有太大变化(如果有的话)。This is almost certainly a false positive. The Leaks instrument is not infallible, and it may mark some things as a leak when they are not (singletons are a common example), see my answer to this question.
I suspect that are seeing here is the creation of the field editor, which is created once for each window containing at least one text field cell. It is lazily created so will only be instantiated when required, i.e. when text editing begins. It is then re-used for all text field editing in that window and is not deallocated until the window goes away. This is exactly the sort of behaviour that can trigger false positives in the Leaks instrument.
Common objects such as
NSTextField
are extremely unlikely to have major memory leaks. They have been very thoroughly tested and do not change much if at all between OS versions.尝试关闭 XCode,再次打开它,然后对项目运行“Clean”。有时,古代死去水手的鬼魂会栖息在 IDE 中,对毫无戒心的旅行者造成严重破坏。但说真的……这似乎不应该发生。尝试清理并重新启动。
Try closing XCode, opening it again and then run a "Clean" on the project. Sometimes the ghosts of ancient dead sailors inhabit the IDE and wreak havoc on unsuspecting travelers. But seriously...that seems like it shouldn't happen. Trying a clean and restart.