NSTouchBar not releasing last touched view

69 Views Asked by At

I am making an app which uses the NSTouchBar. The touchbar is made by the NSWindowController.makeTouchBar()method. In this touchbar I can place NSCustomTouchBarItems. I have made two NSCustomTouchBarItems.

The first one sets a view to a default ui button, with this:

let item001 = NSCustomTouchBarItem(identifier: someIdentifier)
item001.view = NSButton(title: "myButton", target: nil, action: nil)

The second one sets a viewController, with this:

let item002 = NSCustomTouchBarItem(identifier: someIdentifier)
item002.viewController = TestViewController()

The TestViewController only loads a simple view inside its loadView() method.

class TestViewController: NSViewController {
    
    override func loadView() {
        
        self.view = TestView001(frame: NSRect(x: 0, y: 0, width: 100, height: 30))
    }
}

The TestView001 only creates a background color so you can see it. TestView001 has the following code:

class TestView001: NSView {

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        
        print("TestView001.init()")
        
        // Create a background color.
        self.wantsLayer = true
        self.layer?.backgroundColor = NSColor.green.cgColor
    }
    
    required init?(coder decoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

All of this works perfectly.

But when I have touched the second item inside the touchbar, and then close my app's window. The windowController and everything else is nicely released from memory. But I can still see that TestView001 is in memory and not being released.

When using a standard ui button like in item001, then you don't have this problem.

It looks like some NSTouch still has a reference to the view if you look at this image: However, I do not completely understand this image.

enter image description here

What is the best way of solving this. Thanks in advance.

0

There are 0 best solutions below