Tuesday, December 7, 2010

Stack not unwound when exception thrown from native to managed in some cases.

Since our product switched to mixed one, some weird resource leak occurred when exception thrown from native codes and caught in managed codes. After analyzing and reading c++/clr exception model, it was found that the callstack was not unwound in this case, if native c++ is compiled with /Ehsc.

One post can also be found from connect.microsoft.com.
http://connect.microsoft.com/VisualStudio/feedback/details/382860/ehsc-eha-stack-unwinding


At the first glance, it seems to be a bug, but it is by design. Since CLR is based on SEH and /Eha is adopted. C++ exception by default based on /Ehsc, different SEH, so, when access violation occurring, the SEH exception goes different path from c++ exception.

The solution is to compile your native codes with /Eha flag, which is not the default one under VS2008/2005.

Microsoft also mentioned in the above post that this may be a bug, and may be fixed in the next release. Hope to see the updated document.

No comments:

Post a Comment