Marco
Marco

Reputation: 91

NSTextField Leak on Mouse Event

I am experiencing a leak (which I did not see before) on NSTextField (note: macOS, not iOS). I have reproduced it in a simple environment and the leak persists.

I create an app in Xcode, all standard. The only change I make is in the ViewController, where I add a simple NSTextField, as follows:

import Cocoa

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        
        let usernameField = NSTextField(string: "Test")
         
        self.view.addSubview(usernameField)
        usernameField.translatesAutoresizingMaskIntoConstraints = false
        usernameField.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
        usernameField.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
        usernameField.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.8).isActive = true
        usernameField.heightAnchor.constraint(equalToConstant: 36).isActive = true
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }


}

Then I run the app and as soon as the page appears (without interacting with it) I use the Debug Memory Graph in Xcode and there are no leaks.

But, if I click on the text in the NSTextViewController and then run the Debug Memory Graph, there is a leaked object:

enter image description here

Every time I click on the text, a new leak appears (if a click 7 times, there are 7 leaks). Also, in another project, if I change view controller (and the NSTextfield is deallocated), the leaks persist.

I have googled this and found nothing except some references in a header file from many years ago.

I have checked my apps for a long time for leaks, and had never noted this leak before. Could it be due to some changes in Appkit?

Any comment is welcome.

Upvotes: 0

Views: 61

Answers (0)

Related Questions