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

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.
} catch (...) {
// Print internal error error message for other unknown causes.

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
stdext Namespace | Microsoft Docs 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
2. How could I make my application recover from it and exit gracefully?

Thank you!

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:

Thank you!