Paavo Helde
11/24/2008 8:06:00 PM
abhijeet <abhi.10dulkar@gmail.com> kirjutas:
> Hi,
>
> I was looking thru code in my organisation. same dll is sharingcode is
> mixing c++ code & c code, which is being again called by some other c+
> + dll.
>
> Only issue I am not too sure about exception handling in this case -
Passing C++ exceptions through C code is undefined behavior. You can
easily got a segfault or something worse.
The implementation may define the behavior, though. Most notably, MSVC++
does, as C++ exceptions are implemented on top of Windows OS-level
"structured exceptions" which are also supported in C. However, to handle
them properly (like releasing any held resources during stack backwind),
the C code must use non-standard extensions for exception handling by
itself, and I'm by some reason quite sure it often doesn't. I.e. you can
use this technique only in the case when the C interface explicitly
documents that it supports C++ exceptions (that is, on this specific
platform).
For handling C++ exceptions portably, any C++ function which might be
called from C code should catch all exceptions and not let thenm
propagate to the C code. For example, it can convert them into error
codes or log them somewhere else.
hth
Paavo