C++ Terminate Called Without an Active Exception

C++ terminate called without an active exception

When a thread object goes out of scope and it is in joinable state, the program is terminated. The Standard Committee had two other options for the destructor of a joinable thread. It could quietly join -- but join might never return if the thread is stuck. Or it could detach the thread (a detached thread is not joinable). However, detached threads are very tricky, since they might survive till the end of the program and mess up the release of resources. So if you don't want to terminate your program, make sure you join (or detach) every thread.

std::thread - terminate called without an active exception, don't want to 'join' it

The trouble you are encountering is a result of the stopThread going out of scope on the stack. The C++ standard has the following to say about this:

30.3.1.3 thread destructor [thread.thread.destr]

~thread();

If joinable() then terminate(), otherwise no effects. [ Note: Either
implicitly detaching or joining a joinable() thread in its destructor
could result in difficult to debug correctness (for detach) or
performance (for join) bugs encountered only when an exception is
raised. Thus the programmer must ensure that the destructor is never
executed while the thread is still joinable. — end note ]

What this means is that you should not let threads go out of scope without first calling either join() or detach().

The way you describe it, you want the thread to go out of scope without joining so it will continue to run as your application runs. That requires a call to detach(). From there, I can only offer a little wisdom...

  • That thread is now completely responsible for its own lifetime. If it doesn't return on its own, it will run forever (until the process terminates).

  • You are getting user input, presumably from something like cin or getch(). If these are accessed from multiple threads, you do not have much assurance that there are not race conditions in their library implementations. Tread lightly.

C++ Thread: terminate called without an active exception

Your code that creates the thread creates a stack variable that is immediately destroyed. You need to change this:

    if(!isRepeatAllowed)
{
std::thread newThread(getUniqueInteger, arr, i, &newVal);
threadArr.push_back( &newThread);
}

to this:

    if(!isRepeatAllowed)
{
std::thread* newThread = new std::thread(getUniqueInteger, arr, i, &newVal);
threadArr.push_back( newThread);
}

Then uncomment your delete line later on.

Terminate called without an active exception (throw in catch all expression)

I think the message

terminate called without an active exception

actually takes to mean

terminate called without an active C++ exception

terminate() may be called for many different reasons, not limited to unhandled C++ exceptions. The catch (...) clause only catches C++ exceptions. In your particular case, terminate() may be called within allocate_memory() or clone() directly, or by runtime due to unhandled non-C++ exceptions (like some low-level Windows exceptions that should be captured with SEH). In either case, the catch block is never entered.



Related Topics



Leave a reply



Submit