0

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.

Cam
  • 11
  • 2
  • "What is the best way of solving this." What makes one way the best of all? Why do you need to know the best way? If you know one approach, wouldn't that be enough? Where and how do you create an instance of an `NSWindowController`? – El Tomato Oct 02 '21 at 12:03
  • One approach is fine. I create an NSWindowController instance inside my AppDelegate. – Cam Oct 02 '21 at 12:13
  • Is the view released after touching other items? – Willeke Oct 02 '21 at 13:45
  • Yes indeed, the view is then released. For example: if you first touch item002 and then touch item001 (with the standard ui button) the view is then released. – Cam Oct 02 '21 at 13:53
  • Is the problem solved? – Willeke Oct 04 '21 at 08:04
  • No. It's still there unfortunately. – Cam Oct 04 '21 at 08:29
  • Is the view released after touching (between) other items after the window is closed? Why is it a problem when the view stays around a bit longer? – Willeke Oct 06 '21 at 08:14
  • Yes the view is released when you touch other items. The inconsistency is that when you use a standard ui button instead of a custom view, then the standard ui button is immediately released after touching it. The custom view stays in memory when you have touched it. It feels like I have to add something to the custom view so it is released immediately as well. I was only trying to clean up unused memory to make it neat. – Cam Oct 06 '21 at 09:59

0 Answers0