2

I have a Mac app whose applicationWillTerminate:(NSNotification *) handler is not called when the app closes down. There is some c++ memory cleanup code that must be called if the program is shut down, so I need some way to do this.

The reason it doesn't get called seems to be related to my adding the following key to Info.plist:

<key>LSUIElement</key>
<true/>

I've read the other questions and answers on this topic, for example applicationWillTerminate: not being called and applicationWillTerminate not called (the accepted correct answer did not work). applicationDidEnterBackground() is never fired either. applicationDidFinishLaunching() always fires ok. I return NSTerminateNow if applicationShouldTerminate() is ever called, but it isn't called either.

For the curious, the app is a Chrome native messaging host and is launched programmatically. And it can be shut down on me if the pipe closes, so I really need some kind of event or notification or something that will reliably allow me to execute some code on shutdown. My app needs to remain invisible and it needs not to appear in application switcher if the user uses Command-Tab.

UPDATE:

I've noticed from the beginning that the following is logged to the Console app when I terminate my app from the Activity Monitor:

com.apple.xpc.launchd[1]: (com.allinlearning.ailhost.67908[13017]) Service exited due to signal: Terminated: 15

Which relates directly to Peter Hosey's answer for this SO question: applicationWillTerminate does not get invoked. So I immediately added the NSSupportsSuddenTermination key to my Info.plist to disable sudden termination, hoping (as Peter's answer implies) that applicationWillTerminate will be given time to fire. Alas, it did not. But it does perhaps clarify things. If indeed that console statement means my app is being terminated, then how can I get a chance to execute some code before The End?

Community
  • 1
  • 1
Alyoshak
  • 2,696
  • 10
  • 43
  • 70
  • Try adding `[NSProcessInfo.processInfo disableSuddenTermination];` somewhere, that may be biting you. – Gerd K Dec 10 '15 at 00:20
  • @GerdK -- I just tried your recommendation. Interestingly the Console no longer reports that "Terminated" statement. Yet watching Activity Monitor revealed that in a few seconds my app did indeed terminate, with no applicationWillTerminate() notification fired. – Alyoshak Dec 10 '15 at 00:29
  • I [have an app](http://fat-apps.com/tasklog-mac) which uses `LSUIElement` (though it does have an actual UI), and I can confirm that `applicationWillTerminate` is definitely called. You said, "... it can be shut down on me if the pipe closes". Shut down how? If it's getting shut down, say, via SIGTERM (or force quitting with Activity Monitor), `applicationWillTerminate` will almost certainly not be called. – zpasternack Dec 10 '15 at 01:22
  • @zpasternack -- Looks like it is being shut down by a SIGTERM. My app is a Chrome native host app. It is launched automatically by a Chrome extension when a port is opened to it. Then messages can be sent from the Chrome extension to the app via the port. But when the browser tab with the extension is closed, it automatically terminates the native host app that it launched. :( Out of our control, so have to figure something out. I saw an SO answer that said one could "trap SIGTERM using the signal or sigaction functions, which are documented in manpages". I'll post any solution here. – Alyoshak Dec 10 '15 at 01:48
  • Yep, trapping `SIGTERM`, et. al. may well be the way to go. One other thought: if your app is getting killed, do you really need to do any memory deallocation? If it's allocated in your process space, it's all going away anyway, no? – zpasternack Dec 10 '15 at 02:15
  • @zpasternack -- Does it all go away? Isn't memory allocated on the heap lost/leaked if a process dies without freeing it up? – Alyoshak Dec 10 '15 at 16:24
  • @zpasternack -- Indeed you are correct. I'm a little long in the tooth and an answer at this SO post (http://stackoverflow.com/questions/12015409/memory-leaks-and-other-resources-after-a-process-dies) confirmed your statement, mentioning that after NT 3.5 most modern OS's release process resources on exit. I think the SO community would benefit most by an "answer" to my question of "trap SIGTERM ..." while simultaneously mentioning that the process will be cleaned up. Want to add an answer that I can mark as correct? – Alyoshak Dec 10 '15 at 16:54

0 Answers0