I was wondering if there were some ways to exiting/terminate a program abruptly without causing a segfault
or core dump
.
I looked into terminate()
and exit()
and return 0
. They all seem to not work in my project.
if(this->board.isComplete())
{
Utils::logStream << " complete "<< endl;
this->board.display();
exit(0);
//std::terminate();
//abort();
//raise(SIGKILL);
return true;
}
exit()/abort()
and similar functions are usually not the proper way for terminating a C++ program. As you have noticed, they do not run C++ destructors, leaving your file streams open. If you really must use exit()
, then registering a cleanup function with atexit()
is a good idea, however, I would strongly recommend that you switch to C++ exceptions instead. With exceptions, destructors are called, and if there is some top level cleanup to be done before termination, you can always catch the exception on main()
, do the cleanup and then return normally with an error code. This also prevents the code dump.
int main()
{
try
{
// Call methods that might fail and cannot recover.
// Throw an exception if the error is fatal.
do_stuff();
}
catch(...)
{
// Return some error code to indicate the
// program didn't terminated as it should have.
return -1;
}
// And this would be a normal/successful return.
return 0;
}