stdext::bad_alloc not caught by try catch (...)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

stdext::bad_alloc not caught by try catch (...)

jaytee
Hello everyone,

Our application, which is the Clang compiler, can consume a large amount of memory when building very large files. Occasionally it will run out of memory and C++ allocators will throw std::bad_alloc. We added a try/catch around the most top-level function, in order to recover from the out-of-memory exception and issue a user-readable error telling the user that the file is too large to compile:
try {
Success = ExecuteCompilerInvocation(Clang.get());
} catch (const std::bad_alloc &) {
// Print out of memory error message.
exit(1);
} catch (...) {
// Print internal error error message for other unknown causes.
exit(1);
}

The problem is that in some cases of out-of-memory, Clang internal code will throw an stdext::bad_alloc instead of the expected std exception. This exception is not caught by neither of the catch statements and Clang crashes. I did some research around MSDN at https://msdn.microsoft.com/en-us/library/ek139e86(v=VS.100).aspx and I found out that a subset of std has been moved to the stdext namespace.

1. Does anyone know why my try/catch doesn't catch the stdext::bad_alloc exception?
2. How could I make my application recover from it and exit gracefully?

Thank you!
Reply | Threaded
Open this post in threaded view
|

Re: stdext::bad_alloc not caught by try catch (...)

jaytee
Hello everyone!

It seems that we've found the root cause of the problem. The issue was caused by the fact that when unwinding from the first exception (stdext::bad_alloc), destructors are called in LLVM, which try to allocate memory. Because of low memory conditions the new operations inside those destructors also fail and cause other exceptions to be thrown.

This is situation is bad, because you are not allowed to throw exceptions while you unwind from another exception.

Here is a related reference discussing this situation:
http://stackoverflow.com/questions/130117/throwing-exceptions-out-of-a-destructor

Thank you!