What does the following runtime error mean: “terminate called without an active exception\n Aborted”

后端 未结 5 2061
野性不改
野性不改 2021-02-15 05:20

The bug disturbed me about two days: when running the code I have a runtime error of \"terminate called without an active exception\\n Aborted\",why?

I try to locate the

相关标签:
5条回答
  • 2021-02-15 05:24

    The "terminate without an active exception" message is a hint that, at some point in your program, exception handling got broken.

    The memory allocation is probably the primary cause, but probably not the error site. The large allocation will throw a std::bad_alloc exception, and this exception is incorrectly handled somewhere.

    To validate the theory, insert a line like

     throw std::logic_error("Foo");
    

    above the allocation, this should trigger the bug as well.

    I've encountered two common causes for this:

    • Multithreaded mingw programs compiled without the right flags
    • A destructor that was called as part of the stack unwinding process has thrown an exception

    You should be able to diagnose the latter condition with a debugger. A stack trace of your application (e.g. obtained by running it in gdb) should help greatly.

    0 讨论(0)
  • 2021-02-15 05:28

    When I've seen this error, it was caused by the thread object destructing before the thread it encapsulated has exited.

    0 讨论(0)
  • 2021-02-15 05:37

    Like Gearoid Murphy said the error happens when the thread object is destructed before the thread function itself has been fully executed. I detected this error using tinythread library (http://tinythreadpp.bitsnbites.eu/):

    Before:

    #include "tinythread.h"
    ...
    void fork_thread(void (*function_pointer)(void * arg), void * arg_pointer) {
        tthread::thread t(function_pointer, arg_pointer);
        // t is destructed here, causing the "terminate called ..." error
    }
    

    After:

    #include "tinythread.h"
    ...
    void fork_thread(void (*function_pointer)(void * arg), void * arg_pointer) {
        tthread::thread * t = new tthread::thread(function_pointer, arg_pointer);
        // now the new thread object is not destructed here, preventing
        // the "terminate called ..." error. Remember that because thread
        // object must now be destructed explicitly (i.e. manually) with delete
        // call you should store the pointer t to a vector of thread pointers
        // for example.
    }
    
    0 讨论(0)
  • 2021-02-15 05:40

    With MinGW, adding the -mthreads compiler option to gcc solves this problem.

    From the gcc manual:

    -mthreads

    Support thread-safe exception handling on Mingw32. Code that relies on thread-safe exception handling must compile and link all code with the -mthreads option. When compiling, -mthreads defines -D_MT; when linking, it links in a special thread helper library -lmingwthrd which cleans up per thread exception handling data.

    0 讨论(0)
  • 2021-02-15 05:45

    I encountered this when I tried to use throw; outside of a catch clause. The rethrow fails and that error message is displayed.

    0 讨论(0)
提交回复
热议问题