swiftcocoanscolorpanelnscolorwell

How to release NSColorPanel?


I want to make the NSColorPanel launched when the user taps on the NSColorWell. The panel opens when the user taps on the color well, but the instance of the panel is going to be hold even after the user closes the panel.

So I set its releasedWhenClosed to true. However, this time the panel is released properly when the user closes the window, but the next time the user opens the panel by tapping on the same color well, the app crashes because the panel instance has already been gone.

How can I close the window but make the user launch the color well again? Specifically, I set the color well's activate: method to the following:

override func activate(exclusive: Bool) {
    NSColorPanel.sharedColorPanel().showsAlpha = true // the app crashes here the next time the user opens the color well
    super.activate(exclusive)
}

Solution

  • NSColorPanel is a singleton, that is loaded lazily. Only one instance of NSColorPanel may be created. Color panel is loaded to memory on first sharedColorPanel call. If you will release this panel, application will crash next time you will access it, because NSColorPanel class keeps reference to released instance. Apple doesn't provide a way to clear resources after sharedColorPanel usage.