Porting VC++'s __try/__except EXCEPTION_STACK_OVERFLOW to MinGW

Daniel Trebbien picture Daniel Trebbien · Aug 30, 2011 · Viewed 7.2k times · Source

I am trying to port some code using VC++'s try-except statement to MinGW:

bool success = true;

__try {
    //...
} __except ((EXCEPTION_STACK_OVERFLOW == GetExceptionCode())
            ? EXCEPTION_EXECUTE_HANDLER
            : EXCEPTION_CONTINUE_SEARCH) {
    success = false;
    _resetstkoflw();
}
return success;

Is it possible to write code that catches a stack overflow exception using MinGW g++?

Answer

Billy ONeal picture Billy ONeal · Aug 30, 2011

You would need to manually call the Windows API functions which register exception handling; namely, AddVectoredExceptionHandler. Note that by using MinGW which does not respect SEH exceptions, throwing any SEH exception or attempting to catch any such exception will result in undefined behavior, because the normal C++ stack unwinding semantic isn't done. (How does Windows know to nuke all those std::strings on the stack?)

You would also need to call RemoveVectoredExceptionHandler at the end of the time you want that SEH exception handler to be called.

Generally MinGW is lacking in support of Windows features like SEH and COM. Any reason you're trying to use that instead of MSVC++ (given that both compilers are free?)