Search code examples
c++exceptiondllmingwthrift

C++ Exception handling in DLL


I know, that there are lot of similar questions at Stackoverflow. But none of answers can solve my problem.

I am creating DLL which exports some functions this way:

extern "C" __declspec(dllexport) int init() { ... }

I use MinGW 4.4 on Windows XP. There are exceptions in init(), because I use Apache Thrift, and there is code like ttransport->open(), where ttransport is boost::shared_ptr<TTransport>, and TTransport -- Apache Thrift's class. There are possible situations when ttransport->open() throws TTransportException exception (TTransportException inherited from std::exception).

That exception crashes host-program where my DLL is loaded. Host program was written by 3rd person and I have no code of host-program.

Thus, I'm wandering, why wrapper-approaches like this can't help (host program crashes the same way):

try
{
    ttransport->open();        
}
// or just catch(...)
catch (std::exception &e) // or, using TTransportException
{
    return 42;
}

Can someone say what am I doing wrong?

Exceptions are not mine -- all was written in Apache Thrifts library, so I have no choice.


Solution

  • this can only be supported via SEH - most C/C++ compilers offer __try / __except / __finally for this... MINGW is NOT one of them...

    There are some efforts to add support for SEH via macros and/or a library. These are not really production quality, it is more like "alpha code"... so I would refrain from using them without thorough testing (and most likely some modifications) in production...

    Another point is that even if you get this to work on MINGW it might still cause problems with stack unwinding since for that you need compiler support which is not provided by MINGW for SEH - some details see here.