Poco waitForTerminationRequest

I develop a http server based on waitForTerminationRequest.
I enter ctrl+C to kill the server, but some global varaiable's destructor will not be called.

I use a new method, std::exit(EXIT_SUCCESS)
but global destructor will also not be called.

1
2
3
// wait for CTRL-C or kill
waitForTerminationRequest();
std::exit(EXIT_SUCCESS);
The best way to exit while ensuring that destructors are called is to throw an exception of a special type (e.g. ExitException) and catch it at the very highest level, usually in main().
I used 'catch(...)', it is not a exception problem.
In practice, calling std::exit(), calling functions like waitForTerminationRequest() (any function that is not a POF) or throwing any exception from within a signal handler engenders undefined behaviour.

The common subset of the C and C ++ languages consists of all declarations, definitions, and expressions that may appear in a well formed C ++ program and also in a conforming C program. A POF (“plain old function”) is a function that uses only features from this common subset, and that does not directly or indirectly use any function that is not a POF, except that it may use plain lock-free atomic operations. ... The behavior of any function other than a POF used as a signal handler in a C ++ program is implementation-defined.

Note: In particular, a signal handler using exception handling is very likely to have problems. Also, invoking std::exit may cause destruction of objects, including those of the standard library implementation, which, in general, yields undefined behavior in a signal handler.


To handle SIGINT, what is usually done is to set a flag of type std::atomic<> or volatile std::sig_atomic_t from within the signal handler, and check this flag from within the processing loop to exit gracefully.

Note: std::exit (when called from outside a signal handler) does execute destructors of objects with static and thread local storage durations.

Returning from the main function, either by a return statement or by reaching the end of the function performs the normal function termination (calls the destructors of the variables with automatic storage durations) and then executes std::exit, passing the argument of the return statement (or ​0​ if implicit return was used) as exit_code.
http://en.cppreference.com/w/cpp/utility/program/exit
Where is the code you posted? Is it in a signal handler or in a normal function? In the latter case, is it in the main thread or in a different thread?
but global destructor will also not be called.
What makes you think it is not called?
@helios, I used poco library, I have not care about the inner code.

@coder777, I am debugging!
I have not care about the inner code
How do you expect to cause a clean shutdown without even knowing in what context you are?
Topic archived. No new replies allowed.