0

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.

NG_
  • 6,895
  • 7
  • 45
  • 67
  • 1
    don't throw exceptions from C language functions, it's not expected – Cheers and hth. - Alf Dec 15 '12 at 19:53
  • It is a Java exception, you certainly can't catch it with std::exception. You need to use SEH exception handling to catch it. Fairly sure that you cannot make this work when you use MinGW. Backgrounder is here: http://stackoverflow.com/a/7049836/17034 – Hans Passant Dec 15 '12 at 20:46

1 Answers1

1

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.

Community
  • 1
  • 1
Yahia
  • 69,653
  • 9
  • 115
  • 144
  • Some time ago I had also find that resources at http://www.programmingunlimited.net and even used macros support for SEH, but still host program crashes. Is there possibility, that using a library can give some results in my case? – NG_ Dec 17 '12 at 08:25
  • @troyane The code including that library is "alpha code"... you will need to dig deep and modify+test it before even being able to use it in any reliable manner! The only other option is to build an out-of-process server and use IPC. – Yahia Dec 17 '12 at 09:17
  • Yep, think, that I got answer. But, can optimization parameters and, may be other compilation parameters make effect on this exception handling mechanism? – NG_ Dec 17 '12 at 18:10
  • @troyane yes... these can effect the handling mechanisms. – Yahia Dec 17 '12 at 18:11