Crash with Modal Window and NStatusBar Menu

I've got an LSUIElement app with a status bar menu. One of the menu items brings up a modal menu.


If the user does the following the app crashes:

  1. Click on the status bar icon opening the menu.
  2. Select the menu item to show the modal window.
  3. While the modal window is up, click on the status bar icon again.
  4. Close the modal window.


The crash is: Thread 1: EXC_BAD_ACCESS (code=1, address=0x40dedeadbec0)


After googling, the address "0x40dedeadbec0" seems to indicate that an object is getting released twice.


Editing the scheme and enabling Zombie Objects shows the following:

2018-02-21 22:25:48.796200-0500 TestStatusBar[33526:430718] *** -[NSMenuCustomCarbonEventHandler release]: message sent to deallocated instance 0x600000070d80


Running Instruments with the Zombies profile confirms this but doesn't shed any light on where it's coming from.


A simple sample that reproduces that problem is attached. It's implemented entirely in the AppDelegate with a single window with a single close button.


Is this a framework bug?


Thanks,

Greg


----


import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    @IBOutlet weak var window: NSWindow!
    private let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        statusItem.menu = NSMenu()
        statusItem.menu!.addItem(NSMenuItem(title: "Show Modal Window...", action: #selector(showWindow(_:)), keyEquivalent: ""))
        if let button = statusItem.button {
            button.image = NSImage(named: NSImage.Name(rawValue: "StatusBar"))
        }
    }
   
    @objc func showWindow(_ sender: Any) {
        window.center()
        NSApp.runModal(for: window)
    }
   
    @IBAction func closeWindow(_ sender: Any) {
        NSApp.stopModal()
        window.close()
    }
   
    override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
        guard NSApp.modalWindow == nil else {
            /
            return false
        }
       
        /
       
        return true
    }
}
Crash with Modal Window and NStatusBar Menu
 
 
Q