473,386 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

/clr and exceptions from native libs

I'm seeing incorrect (I think) behavior in the event of an exception
thrown in a native C++ library. Basically, the stack appears to
unwind correctly if the caller is native, but incorrectly if the
caller is /clr. Consider the following C++ static lib:

// ehTestLib.h
#include <iostream>
class worker {
public:
~worker() { std::cerr << "~worker()\n"; }
void doTheThingThatThrows();
};

class supervisor {
public:
~supervisor() { std::cerr << "~supervisor()\n"; }
void supervise();

private:
};

class director {
public:
~director() { std::cerr << "~director()\n"; }
void direct();
};

// ehTestLib.cpp
#include "ehTestLib.h"
void worker::doTheThingThatThrows() {
throw "";
}

void supervisor::supervise() {
worker w;
w.doTheThingThatThrows();
}

void director::direct() {
supervisor().supervise();
}

I build this lib via:
cl /c /EHsc /MD ehTestLib.cpp
link /out:ehTestLib.lib ehTestLib.obj
I test this library with the following code:
// ehTestApp.cpp
#include "ehTestLib.h"
int main() {
try {
director().direct();
}
catch (...) {
}
}

The expected output is, obviously:
~worker()
~supervisor()
~director()
Now, when I build ehTestApp.cpp as native C++:
cl /c /EHsc /MD ehTestApp.cpp
link /out:ehTestApp.exe ehTestLib.lib ehTestApp.obj

I get the expected output. However, when I build it as managed C++:
cl /c /clr /EHa /MD ehTestApp.cpp
link /out:ehTestApp.exe ehTestLib.lib ehTestApp.obj

I get the following only:
~director

Two other tidbits:
1. The problem as something to do with the throw being in a separate
library. If I simply include the code directly in the exe, everything
works as expected in native and /clr.
2. The native example works the same with /EHa or /EHsc.

I'm hoping someone out there has some insight as to why this is
happening. I've read through all of the related MSDN stuff (starting
with http://msdn2.microsoft.com/en-us/lib...h(VS.80).aspx),
but I don't see any mention of this.

Thanks in advance,
Ryan
Nov 29 '07 #1
4 1658
Two other tidbits:
1. The problem as something to do with the throw being in a separate
library. If I simply include the code directly in the exe, everything
works as expected in native and /clr.
2. The native example works the same with /EHa or /EHsc.
Another thing I just discovered: this all works as expected with the
7.1 version of the compiler. The incorrect behavior I describe only
occurs in 8.0 (VS 2005).

Ryan

Nov 29 '07 #2

"rwf_20" <rf****@gmail.comwrote in message
news:f5**********************************@w28g2000 hsf.googlegroups.com...
I'm seeing incorrect (I think) behavior in the event of an exception
thrown in a native C++ library. Basically, the stack appears to
unwind correctly if the caller is native, but incorrectly if the
caller is /clr. Consider the following C++ static lib:
I see you compiled the host with both /EHsc and /EHa, but did you try the
library with /EHa?
>
// ehTestLib.h
#include <iostream>
class worker {
public:
~worker() { std::cerr << "~worker()\n"; }
void doTheThingThatThrows();
};

class supervisor {
public:
~supervisor() { std::cerr << "~supervisor()\n"; }
void supervise();

private:
};

class director {
public:
~director() { std::cerr << "~director()\n"; }
void direct();
};

// ehTestLib.cpp
#include "ehTestLib.h"
void worker::doTheThingThatThrows() {
throw "";
}

void supervisor::supervise() {
worker w;
w.doTheThingThatThrows();
}

void director::direct() {
supervisor().supervise();
}

I build this lib via:
cl /c /EHsc /MD ehTestLib.cpp
link /out:ehTestLib.lib ehTestLib.obj
I test this library with the following code:
// ehTestApp.cpp
#include "ehTestLib.h"
int main() {
try {
director().direct();
}
catch (...) {
}
}

The expected output is, obviously:
~worker()
~supervisor()
~director()
Now, when I build ehTestApp.cpp as native C++:
cl /c /EHsc /MD ehTestApp.cpp
link /out:ehTestApp.exe ehTestLib.lib ehTestApp.obj

I get the expected output. However, when I build it as managed C++:
cl /c /clr /EHa /MD ehTestApp.cpp
link /out:ehTestApp.exe ehTestLib.lib ehTestApp.obj

I get the following only:
~director

Two other tidbits:
1. The problem as something to do with the throw being in a separate
library. If I simply include the code directly in the exe, everything
works as expected in native and /clr.
2. The native example works the same with /EHa or /EHsc.

I'm hoping someone out there has some insight as to why this is
happening. I've read through all of the related MSDN stuff (starting
with http://msdn2.microsoft.com/en-us/lib...h(VS.80).aspx),
but I don't see any mention of this.

Thanks in advance,
Ryan

Nov 29 '07 #3
On Nov 29, 1:16 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
I see you compiled the host with both /EHsc and /EHa, but did you try the
library with /EHa?
Never thought to try that, but it fixes the issue. Thanks for the
tip. I'm curious as to why it's now necessary to enable SEH in my
native libs? Something to do with the way the exceptions are handled
in the presence of the native/managed 'bridge'?

Are there any side-effects to using /EHa that I should know about?

Thanks again,
Ryan
Nov 29 '07 #4

"rwf_20" <rf****@gmail.comwrote in message
news:e0**********************************@e25g2000 prg.googlegroups.com...
On Nov 29, 1:16 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
>I see you compiled the host with both /EHsc and /EHa, but did you try the
library with /EHa?

Never thought to try that, but it fixes the issue. Thanks for the
tip. I'm curious as to why it's now necessary to enable SEH in my
native libs? Something to do with the way the exceptions are handled
in the presence of the native/managed 'bridge'?
Because it forces the C++ compiler to use the OS exception support. The
..NET runtime only handles OS exceptions (SEH), so a /EHsc C++ exception
(which may be stored in an internal variable in the C++ runtime library)
won't be seen by the catch blocks, and hence the unwinding won't be done.

The stronger rule is to never throw exceptions across language boundaries,
because exceptions are language-specific (think MSIL for .NET, not
C#;VB.NET;F#;JavaScript.NET;C++/CLI;IronPython).

Possibly the C++/CLI compiler should catch both synchronous native C++
exceptions and SEH-based exceptions, but apparently it doesn't.
>
Are there any side-effects to using /EHa that I should know about?
Some optimizations aren't possible (removing try-blocks around functions
that can't throw, that sort of thing).

Also catch (...) clauses will catch non-C++ exceptions.
>
Thanks again,
Ryan

Nov 29 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: calin.hanchevici | last post by:
Hi all, I have a C++ library I call from python. The problem is I have c++ exceptions that i want to be translated to python. I want to be able to do stuff like: try: my_cpp_function() except...
24
by: mag31 | last post by:
Is there any way to find out if a particular .net function will throw an exception without first generating the exception? I am using structured exception handling i.e. try catch finally blocks...
11
by: Steven Blair | last post by:
Hi, I need to catch exceotions on File.Delete() After checking the help, I have noticed that thgere are serevral Exceptions that can be thrown. My question is, should I catch all thes...
2
by: yaron | last post by:
Hi, How do i catch an unmanaged c++ exceptions ? right now i can catch the System.Runtime.InteropServices.SEHException and System.Exception exceptions , but i wan't to catch myException...
8
by: Richard Collette | last post by:
When attempting to debug a webservice, I get the error: Cannot serialize member System.Exception.Data of type System.Collections.IDictionary, because it implements IDictionary. In reading...
19
by: docschnipp | last post by:
Hi, as someone pulling to C# over from C++ I stumbled over something today for which I was not able to find an answer: HOW do I convert a byte block with a given endianess to an int32 and vice...
18
by: Denis Petronenko | last post by:
Hello, in the following code i have segmentaion fault instead of exception. Why? What i must to do to catch exceptions in such situation? Used compiler: gcc version 3.3.6 (Debian 1:3.3.6-13) ...
6
by: Henryk | last post by:
I did a lot of delphi GUI programming recently. In my experience most of the time you just want to throw a standard exception with a descriptive message. Then all calling functions can handle...
1
by: dragonslayer008 | last post by:
I read in a C++/CLI book that: "In C++/CLI, exceptions are always thrown on the managed heap, never the stack." So I'm wondering if this will be a problem for me. Here is my situation. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.