Re: how do I save prefs when the app is Force Quit?
Re: how do I save prefs when the app is Force Quit?
- Subject: Re: how do I save prefs when the app is Force Quit?
- From: Greg Parker <email@hidden>
- Date: Sun, 14 May 2006 12:52:09 -0700
Alexander Dymerets wrote:
Alan Smith wrote:
I need to save prefs but want it to be done even if the app is force
quit. Does anyone know how?
void catch_term(int sig_num)
{
if (sig_num!=SIGTERM) return;
[myPreferences save];
exit(0);
}
int main(int argc, const char *argv[])
{
signal(SIGTERM, catch_term);
return NSApplicationMain(argc, argv);
}
Don't do that. Your program may hang.
The list of "things you're allowed to do in a signal handler" is
short. Sending Objective-C messages is not one of them. Neither is
calling exit() or doing anything with CFPreferences.
The problem is locks. A signal interrupts a thread, and the
interrupted thread may be holding locks. If the signal handler tries
to use the same locks, it deadlocks. For example, malloc() uses locks.
The program will hang if the signal interrupts a malloc() call and
then calls malloc() itself.
objc_msgSend() may take locks and may call malloc(), so it can't be
used from a signal handler. CFPreferences may call malloc() and uses
its own locks internally, so you can't call it either. exit() invokes
atexit handlers, which may do arbitrary unsafe things.
You can find a list of signal-safe functions in the sigaction(2) man
page. In particular, you should use _exit() and write() instead of exit
() and printf().
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden