Reputation: 21
I am working with a program where my code calls a third party library which uses boost and shared_pointers to create a large and complex structure. This structure is created in a method that I call and at the end of the method I know that the program is finished. For a large sample that I am handling the code to handle the processing takes 30 minutes and the boost code called automatically at exit takes many hours. Exiting the program without releasing the memory and spending all that time would be a perfectly acceptable outcome. I tried vector *iddListV = new vector(); // this WILL leak memory with all the relevant structures added to the vector but this does not help. I also tried calling exit(0); before reaching the end of the subroutine. This also causes the boost code to spend many hours trying to release pointers. How to I get a C++ program (Microsoft C++ on Windows if that matters) to abruptly exit without calling the boost destructors. My constraints are I can call any function before the boost structure are allocated but cannot modify the code once it starts running.
Upvotes: 0
Views: 94
Reputation: 4590
It is possible, in a portable manner, to do:
#include <exception>
...
std::terminate();
However, there's a big gotcha, in that, at least on linux, this may cause a core dump. (I'm really not sure what the behavior is on Windows).
It should be noted, that the behavior is implementation defined as far as whether or not destructors are called. Siting §15.5.1 P2:
In the situation where the search for a handler (15.3) encounters the outermost block of a function with a noexcept-specification that does not allow the exception (15.4), it is implementation-defined whether the stack is unwound, unwound partially, or not unwound at all before std::terminate() is called.
Additionally in §18.8.3.4 P1:
Remarks: Called by the implementation when exception handling must be abandoned for any of several reasons (15.5.1), in effect immediately after evaluating the throw-expression (18.8.3.1). May also be called directly by the program.
C++11 also defines the function std::quick_exit(int status)
that can be used in a similar manner (presumably without a coredump). This function is available from <cstdlib>
.
Upvotes: 0
Reputation: 4439
If you're unconcerned about portability, you can call TerminateProcess()
. But remember to take care that you are absolutely sure that your program is in a state which is ready to terminate. For example, if you terminate before I/O has had a chance to flush, then your file data and network streams may become invalid.
Upvotes: 0