1

I am using a main file and an AppDelegate with the code below. The issue occurs when I try to quit my app from the Dock. When I right-click on the app icon and choose "Quit", it crashes with the error "Thread 1: signal SIGTERM" at the line nsApplication.run().

Although I have implemented deinit, applicationShouldTerminate, and applicationWillTerminate, none of them print anything to the Xcode console when the crash happens. However, sometimes, completely at random, the app quits correctly using the Quit option from the Dock, and I see the expected prints. Most of the time, though, the app crashes when trying to quit. My ContentView is just a simple "Hello, World!" view. How can I solve this issue?

import Cocoa

let nsApplication: NSApplication = NSApplication.shared
let appDelegate: AppDelegate = AppDelegate()
nsApplication.delegate = appDelegate
nsApplication.run()

import Cocoa
import SwiftUI

class AppDelegate: NSObject, NSApplicationDelegate {
    
    private lazy var window: NSWindow = NSWindow()
    
    deinit {
        print("AppDelegate is being deallocated")
    }
    
    func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
        
        print("terminateNow")
        return .terminateNow
    }
    
    func applicationWillTerminate(_ notification: Notification) {
        print("Application will terminate")
    }

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        window.styleMask = [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView]
        window.backingType = .buffered
        window.isReleasedWhenClosed = false
        window.center()
        window.setFrameAutosaveName("Main Window")
        window.title = "Window"
        window.isMovableByWindowBackground = true
        window.contentView = NSHostingView(rootView: ContentView())
        window.makeKeyAndOrderFront(nil)
    }
}

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            Text("Hello, world!")
        }
        .frame(width: 400.0, height: 200.0)
    }
}
1
  • 1
    Even the Xcode macOS app templates without any changes have this problem. It's a fairly recent issue, it worked fine in previous versions of macOS/Xcode. Commented Feb 12 at 7:02

1 Answer 1

-1

It seems not an issue since you meant to quit the app. It only happens because you using Xcode to debug. In other words, it just quit as you expect. Check this: here

Sign up to request clarification or add additional context in comments.

5 Comments

But why when I run this code in a button as an action it quit the app correctly? "NSApplication.shared.terminate(nil)" without issue? Also I am not using Xcode to debug, I did not say that in my issue, I am running the codes and quitting the app from dock. Also it is very annoying having crash on quit while building the app. If I create and use standard SwiftUI project using quit from dock does also make the app to crash.
@Mango If you not using Xcode then how did you get the crash information?
I am using Xcode, I did not debug the issue, since the error does not help to debug that is why we are talking now
@Mango It doesn't matter what did you use Xcode for. I just trying to say if we don't have these Xcode stuff, the app just quit as you want it to quit, there has nothing wrong. You only get these "crash" message while you using Xcode.
@Tyler This is a real issue. My app shouldn't crash when I quit from the Dock and it's annoying when I have to go to Xcode to kill it. It has nothing to do with the simulator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.