only menu item Mac app

Prior to Ventura, my app that only has a Menu Item does not have a Storyboard but does have a custom main.swift as

import Foundation
import Cocoa

let app = NSApplication.shared
let delegate = AppDelegate()
app.delegate = delegate

_ = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)

And then in Info.plist, I added Application is agent (UIElement) YES so the app isn't displayed in the dock.

Occasionally, the app now crashes on start.

Is there documentation for creating a Menu item only macOS app without Storyboard?

It's typically crashing in _NSCGSDisplayConfigurationUpdateAndInvokeObservers or in _NSCGSDockMesageReceive.

Thanks,

Answered by parker9 in 742614022

Turns out it has nothing to do with main.swift. I have a dictionary defined as dict : [ CGWindowID : NSWindow]. if you do dict.removeValue(forKey: key)?.close() this double deallocates the NSWindow if dict[key] was the last strong reference.

Found this by instead of Run in Xcode, you do Profile and select Zombie and I found that this NSWindow reference count goes to -1. oops.

Should have said "Status menu item" instead of "menu item". Regardless, changed main.swift to this:

import Cocoa

let app = NSApplication.shared
let delegate = AppDelegate()
withExtendedLifetime(delegate, {
        app.delegate = delegate
        app.run()
        app.delegate = nil
  })

seems to fix the occasional crash. This just forces a hard link to AppDelegate() so it doesn't get deallocated, apparently.

Nope, same crash as before....

Accepted Answer

Turns out it has nothing to do with main.swift. I have a dictionary defined as dict : [ CGWindowID : NSWindow]. if you do dict.removeValue(forKey: key)?.close() this double deallocates the NSWindow if dict[key] was the last strong reference.

Found this by instead of Run in Xcode, you do Profile and select Zombie and I found that this NSWindow reference count goes to -1. oops.

only menu item Mac app
 
 
Q