Is it possible to terminate software execution without skipping calls to destructors? For instance, in the code below, the destructor for test will never be called because of the exit(1) statement.
#include <iostream>
#include <cstdlib>
using namespace std;
class A{
public:
A(){cout << "Constructed.\n";}
~A(){cout << "Destroyed.\n";}
};
void func()
{
//Assuming something went wrong:
exit(1);
}
int main(int argc, char *argv[])
{
A test;
func();
return 0;
}
What I need, is a way to end the program (from within func()) that calls all necessary destructors before terminating. So far I've been handling this through func() return value, as in:
bool func()
{
//Assuming something went wrong:
return false;
}
int main(int argc, char *argv[])
{
A test;
if( !func() )return 1;
return 0;
}
The problem with this method is that it quickly becomes very annoying (and code bloating) to manage once you need to apply it to a series of nested functions.
Is there a way of achieving the same results of the second example (proper destructor calls) with a syntax similar to the first example (call exit(1) wherever you are)?
Throw an exception, catch it in main and return.
This relies on nothing else catching your exception without rethrowing it.
You could rely on stack unwinding for this: when you want to exit, throw an exception and catch it in main().
struct my_exit
{
int error;
int operator()()
{
// do any cleanup on globals
return error;
}
};
int main()
{
try
{
doSomethingThatCouldCauseExit();
}
catch (my_exit & me)
{
// Clean up globals now
exit(me());
}
}
There are several ways to do this cleanly.
One solution is to use the atexit function, which simply calls the given function pointer when the program terminates.
You would have to allocate all of your objects off the heap, maintain some global table with pointers to all instantiated class instances, and then simply iterate through the table delete ing each instance in the registered function.
Related
I have a loop of functions that I called like the code I wrote below and I want at some point to end the program.The problem is that I don't want to use exit function because I have data allocated dynamic also I could use an if in every function and exit one by one but I think will make the code a lot harder to understand and I have more than 3 functions like this.
void c()
{
//code
//I want to exit the program
}
void b()
{
c();
//code
}
void a()
{
b();
//code
}
int main()
{
a();
return 0;
}
Thanks for the help.
You can return from all functions all the way to main (nicest).
You can call some variant of exit.
You can throw an exception.
You can use setjmp/longjmp to jump to the end of main (please don't).
You can crash the application (by calling abort, raise(SIGKILL) or similar).
I can't think of more options, but there may well be some...
Inside your functions used std::unique_ptr wherever you need dynamic allocations.
Then modify :
int main()
{
try
{
a();
}
catch(...)
{ /*... */}
return 0;
}
Then inside any of the deep functions, throw can be used, and std::unique_ptr will release the resources auto-magically.
I was recently asked this question in an interview. Below code, snippet behavior was asked. I mentioned that the code would
throw an exception that would be caught but it did not turn out to be the right answer.
I have tried debugging this piece of the code snippet and have two questions.
1.
If execution does enter the line try { A a; } then why catch fails to catch the exception?
2.
What is the behavior of this orphan piece of code that does not belong to any method inside the struct? If I put the orphan code
to be included under the constructor method of B i.e inside B() { } then the exception is caught properly. so how does the execution
flow handle this? I mean to which method/function stack does the orphan code try catch in B belong?
#include<iostream>
#include<string>
using namespace std;
struct A
{
A()
{
throw 2;
}
};
struct B
{
B()
// start of orphan code.
try
{
A a;
}
catch(int i)
{
cout << i << endl;
}
// end of orphan code.
};
int main(int argc, char* argv[])
{
B b;
cout << "3" << endl;
return(0);
}
What you call "orphan code" is in fact a function try block. Such blocks have a special behavior when used with constructors and destructors: every catch block implicitly rethrows the exception, as if the last statement were throw; . This is done to make it impossible to use an object that failed to complete construction.
I would like my C++ code to stop running with proper object cleanup if a certain condition is met; in a constructor of a class.
class A {
public:
int somevar;
void fun() {
// something
}
};
class B {
public:
B() {
int possibility;
// some work
if (possibility == 1) {
// I want to end the program here
kill code;
}
}
};
int main() {
A a;
B b;
return 0;
}
How can I terminate my code at that point doing proper cleanup. It's known that, std::exit does not perform any sort of stack unwinding, and no alive object on the stack will call its respective destructor to perform cleanup. So std::exit is not a good idea.
You should throw an exception, when the constructor fails, like this:
B() {
if(somethingBadHappened)
{
throw myException();
}
}
Be sure to catch exceptions in main() and all thread entry functions.
Read more in Throwing exceptions from constructors. Read about Stack unwinding in How can I handle a destructor that fails.
It is not possible to perform just from a constructor. If you throw an exception then applications need to set up a proper exception handling code at entry points, because if you just thrown an exception that won't be handled then compiler is allowed to skip stack unwinding and cleanup.
If you don't want to use use exceptions, you can have an init method in class B that returns a return code:
class B {
public:
B(/*parameters that will be used by init*/) : ...
int init(); // actually initialize instance and return non 0 upon failure
}
In the code below i use try/catch in the python module code. In the try block i have a simple error (memory access violation) and trying to catch the corresponding exception and to terminate the program quietly without generation of the .stackdump file. However the latter is still generated what implies that try/catch construct does not do its job. How could i avoid generating .stackdump file and exit the program without errors when the improper operation (like one in the code) is met?
P.S. i'm compiling the code in cygwin with gcc and boost.python
It is interesting that it doesn't work only in case x[3]=2, but works for all other cases: e.g. x[4]=2 or x[20]=2 or, obviously, x[2]=2.
#include <boost/python.hpp>
#include <iostream>
#include <iomanip>
using namespace std;
using namespace boost::python;
class Hello
{
std::string _msg;
public:
Hello(std::string msg){_msg = msg;}
void run(){
try{
double* x;
x = new double[3];
x[3] = 2.0;
delete [] x;
}catch(...){ exit(0); }
}
};
BOOST_PYTHON_MODULE(xyz)
{
class_<Hello>("Hello", init<std::string>())
.def("run",&Hello::run)
;
}
EDIT:
According to what Maciek has suggested i tried the following trick:
Make signal handling function to throw an exception, but not exit
void sig_action(int signo) {
std::cout << "SIGNAL " << signo << std::endl;
throw 1;
// exit(0);
}
And now try to enclose a possibly problematic function in try/catch block (signal function is placed in class constructor):
class Hello
{
std::string _msg;
public:
Hello(std::string msg){
_msg = msg;
signal(SIGABRT, sig_action);
signal(SIGSEGV, sig_action);
}
void set(std::string msg) { this->_msg = msg; }
std::string greet() { return _msg; }
void run(){
try{
double* x;
x = new double[3];
x[3] = 2.0;
delete [] x;
}catch(...){ cout<<"error in function run()\n"; exit(0); }
}
};
However such a trick doesn't work as i expected it produces the following output:
SIGNAL 6
terminate called after throwing an instance of 'int'
SIGNAL 6
terminate called recursively
SIGNAL 6
terminate called recursively
....
(and many more times the same)
So the exception is thrown, but everything finishes before it has been caught. Is there any way to let it be caught before terminating the process?
You can only catch exceptions that are thrown. An invalid pointer access doesn’t throw an exception, it simply causes undefined behaviour, and in your particular case it results in a stack dump.
If you want to catch such a situation situation, use std::vector and the at function to access items. This will throw std::out_of_range when used with an invalid index. However, it’s usually better to avoid the possibility of such accesses a priori since they are usually indicative of a bug in your program, and bugs should not be handled via exceptions, they should be removed from the code.
On linux core dumps are generated by signal handlers with default action set to core (SIGABRT, SIGSEGV, ...). If you want to avoid core dump you can always capture/ignore those signals. It should work on Cygwin stackdumps as well. But you will still probably get some nasty message as output.
EDIT:
#include <signal.h>
// [...]
void sig_action(int signo) {
std::cout << "SIGNAL " << signo << std::endl;
exit(0);
}
int main(int argc, char* argv[]) {
signal(SIGABRT, sig_action);
signal(SIGSEGV, sig_action);
Hello h("msg");
h.run();
}
I want to call destructor of an instance (proc) always before my program ends, especially after return 1 or exit() in main.
I found C++ function atexit(), but it requires pointer to void function with no argument, so the code below cannot be compiled. How I can solve it, please?
Destructor of my instance requires MySQL connection.
#include <WinSock.h>
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <mysql.h>
#include <string>
// Declarations for Mysql DB
using namespace std;
class Process {
public:
~Process();
};
Process::~Process ()
{
// Interaction with DB
}
int main(void)
{
// Join to DB
atexit(proc.~Process); // Call desctructor of instance proc before program ends
Process proc;
// App code
return 0;
}
proc has automatic duration, i.e. when exiting main, it will be destroyed automatically (and the destructor invoked) - you don't need the atexit business..
Unless as #Rob mentions below, you call exit() in your code somewhere... if that's the case, then you'll have to allocate Process on the heap, provide a function that atexit can call which is aware of this instance, and delete it there...
Just make it a global std::auto_ptr<> or std::unique_ptr<>:
std::auto_ptr<Process> proc; // 1) initialized with NULL before main() is called
int main() {
proc.reset(new Process); // 2) re-initialized
}
// 3) proc destructor is called after main() exits
Use C++:
#include <memory>
std::unique_ptr<Process> g_proc;
int main()
{
g_proc.reset(new Process(some, runtime, params));
// all done!
}
Objects of static storage duration (e.g. globals, like our g_proc here) are destroyed after main exits, and the destructor of unique_ptr will take care of the object destruction.
Alternatively you can make g_proc a static variable inside main, though that's a bit unusual.
Change your program logic slightly to allocate your Process object dynamically:
Process *pProc;
void killProc() {
delete pProc;
}
int main(void)
{
// Join to DB
atexit(killProc); // Call desctructor of instance proc before program ends
pProc = new Process();
Process& proc = *pProc;
// App code
return 0;
}
As proc is not a pointer, it will be automatically deleted at the end of the main() function, and it's destructor will be called (before the memory is deallocated).
The atexit() function is not a C++ function, but is part of the standard C library.
If you need to call the destructor BEFORE the end of the main() function, you need to allocate the proc variable as a pointer.
You can also avoid the usage of global variables plus C functions by using an application class this way:
class Application
{
public:
Application() { proc = new Process(); /* other init code */ }
~Application() { delete proc; /* other clean-up code */ }
int run()
{
/* your application code goes here */
}
private:
Process *proc;
}
int main()
{
Application app;
int result = app.run();
/* Post clean-up code */
return result;
}
If you plan using C++11 can also rely on the 'unique_ptr' template. Avoid using 'auto_ptr' since it is deprecated.