[cfe-dev] stdext::bad_alloc not caught by try catch (...)

jaytee aloha_arts at yahoo.com
Fri May 29 01:52:32 PDT 2015

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

Thank you!

View this message in context: http://clang-developers.42468.n3.nabble.com/stdext-bad-alloc-not-caught-by-try-catch-tp4045778.html
Sent from the Clang Developers mailing list archive at Nabble.com.

More information about the cfe-dev mailing list