NSTextField leaking when handling key events

2019-07-01 14:13发布

问题:

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.

回答1:

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.



回答2:

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.