views:

189

answers:

1

I have some code that needs to be run when the application terminates. I register my controller for the NSApplicationWillTerminateNotification as follows:

[[NSNotificationCenter defaultCenter] 
    addObserver: self
       selector: @selector(applicationWillTerminate:)
           name: NSApplicationWillTerminateNotification
         object: nil];

Now, if I start my app and quit it within the first 20 seconds or so, applicationWillTerminate gets called. If I quit the application later, it doesn't. What in my application could cause this behaviour? I have also tried to set up my controller as NSApplication's delegate with same results. Any ideas?

Thanks.

Oh, and this is XCode 3.2, Snow Leopard 10.6.1, using 10.5 SDK. Happens in both Debug and Release builds.

+4  A: 

There are several reasons why this might be happening.

If you are running GC'd, does your observer get collected and finalized before the termination happens? (I should test this and file a bug if it does as that at least needs to be documented)

Is your app silently crashing or calling exit() directly?

In general, you cannot count on the termination notification being received as the user might go for a force quit for the heck of it.

Also, in Snow Leopard, there is a feature called sudden termination that allows your app to let the system know that it is safe to kill the app instead of going through the normal termination rigamarole. It is documented in the NSProcessInfo documentation.

bbum
Yes, this behaviour was caused by the 'sudden termination' feature. I had NSSupportsSuddenTermination enabled in my Info.plist. Many thanks!
svintus