void Class::CleanUp( int signum )
{
if(pInstance != NULL)
{
delete pInstance;
pInstance = NULL;
}
exit(1);
}
this guy gets called because of this signal: (void) signal( SIGINT, CleanUp );
A SIGINT signal is a "control-c" keypress and pInstance is the instance of my class because it is a singleton.
A couple of questions:
Is exit(1) a good idea? if not, what are my other options?
Is exiting your app outside of main() smart, or a bad idea?
If this looks like a bad concept, how else would one go about doing this?
exiting simple c++ app inside a class
I need to write a c++ class that has a "clean up" method invoked on a "control-c" keypress. It's a singleton and the method looks like this:
If your main application loop has access to this object, just give the object a conditional interface function. ie..
bool Class::ReadyToExit(void) const { return m_ExitNow; }
edit: You would have to call this before you deleted it, of course.
bool Class::ReadyToExit(void) const { return m_ExitNow; }
edit: You would have to call this before you deleted it, of course.
When you call exit(), any objects that have not been destroyed will not have their destructors run. You would have to ensure that all your objects have been properly destroyed before you can depend on exit().
Returing from main() is preffered.
As for how to handle it, if your app has a main loop that is in main() or called from main() just set some variable on CTRL-C to break the loop and return into main() and then return from main() normally.
Returing from main() is preffered.
As for how to handle it, if your app has a main loop that is in main() or called from main() just set some variable on CTRL-C to break the loop and return into main() and then return from main() normally.
Quote:Original post by rip-off
When you call exit(), any objects that have not been destroyed will not have their destructors run. You would have to ensure that all your objects have been properly destroyed before you can depend on exit().
Eh? I thought that destructors of already constructed objects get run when you call exit, which is why you should never ever call exit from a destructor (infinite recursion).
I thought that abort() was the function that doesn't call destructors myself.
[EDIT: typed constructors rather than destructors, duh]
Quote:Original post by Paradigm Shifter
Eh? I thought that destructors of already constructed objects get run when you call exit
Only the destructors of static objects get called when you call exit(). Here's an example
#include <cstdlib>#include <iostream>class foo{public: ~foo() { std::cout << "foo's destructor called" << std::endl; }};class bar{public: ~bar() { std::cout << "bar's destructor called" << std::endl; }};int main(){ foo a; static bar b; exit(EXIT_SUCCESS);}
outputs:
bar's destructor called
I'll take your word for it.
But using cout in a global destructor... tsk. What if cout has been destroyed? Power to the printf!
But using cout in a global destructor... tsk. What if cout has been destroyed? Power to the printf!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement