I am not very sure whether this problem has been discussed here before.I am very sorry if it has already been discussed.
The issue :
I have some serious clean up work to do before my JUCE application quits.This is fine as long as my application quits normally.What if it crashes ? I cannot afford NOT to do the clean up in any case.
Yes, the code has to be “exception handled”, but in an event of an unhandled exception it becomes an issue.
I tried over-ridding the “unhandledException” function from the JUCEApplication class.In the event of any crash ( i had to force generate this crash ) the control comes to this function on Windows and I could safely do my clean up.
But on MAC, the control just does not come to this function.Is this known behavior ? How do I then do the required clean up in the event of a crash on MAC ?
Any help in this regard would be greatly appreciated.
Yes, it’s true that you don’t get the same exception catching abilities on the mac.
On the other hand, that’s not a bad thing, because it encourages you to be more vigilant about crashing! It’s actually a bit worrying on windows that you can do something nasty and carry on with possibly corrupted memory everywhere…
I am not very sure whether this problem has been discussed here before.I am very sorry if it has already been discussed.
The issue :
I have some serious clean up work to do before my JUCE application quits.This is fine as long as my application quits normally.What if it crashes ? I cannot afford NOT to do the clean up in any case.
Yes, the code has to be “exception handled”, but in an event of an unhandled exception it becomes an issue.
I tried over-ridding the “unhandledException” function from the JUCEApplication class.In the event of any crash ( i had to force generate this crash ) the control comes to this function on Windows and I could safely do my clean up.
But on MAC, the control just does not come to this function.Is this known behavior ? How do I then do the required clean up in the event of a crash on MAC ?
Any help in this regard would be greatly appreciated.
Thanks in advance,
Anil.[/quote]
Yeah, the world of unix…
You set up a signal handler:
#include <signal.h>
pid_t mainpid;
void finish(int sig){
if(getpid()==mainpid){
fprintf(stderr,"Hey, I was shut down!\n");
}
exit(0);
}
int main(void){
mainpid=getpid();
signal(SIGINT,finish);
...
}
The above is good enough for most uses, but if you are really serious, you fork() your program, and let the parent monitors the status of the child.
[/u]
Wow ! A cool idea…But my doubts on this solution :
1 ) Since START_JUCE_APPLICATION is used , there is no access to the main function directly in a JUCE application , so where do I actually register the handler for SIGINT.In App::initialise ?
2 ) Now think of a scenario where I am loading an image using the ImageFileFormat::loadFrom function where the image file actually does not exist.So the image is NULL.What happens when I call image->getHeight() ( or any other function on any NULL pointer ) .Will it come to the handler ?
I tried and it did not work.
( This is of course in the backdrop of the assumption that the exception is unhandled by the app )
Wow ! A cool idea…But my doubts on this solution :
1 ) Since START_JUCE_APPLICATION is used , there is no access to the main function directly in a JUCE application , so where do I actually register the handler for SIGINT.In App::initialise ?
[/quote]
You don’t have to do it in main.
No it doesn’t. Try SIGSEGV instead of SIGINT. (But you need a SIGINT handler as well, since that is the way to signal the process to stop)
I’m not a mac expert. But since its a UNIX, its likely, that you can allways kill a program just by calling “kill -9 ”. And when you do that (which quite often is done by the user, since its a safe way to ensure the process stops), there is no way your program can know it has been shut down.
In that scenario, you have to use fork() or other mechanisms where another process monitors your program.