Sign in to follow this  

exiting simple c++ app inside a class

This topic is 4073 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 4073 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this