R
rwf_20
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/library/x057540h(VS.80).aspx),
but I don't see any mention of this.
Thanks in advance,
Ryan
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/library/x057540h(VS.80).aspx),
but I don't see any mention of this.
Thanks in advance,
Ryan