Answered by:
Throwing exceptions across dll boundaries in C#

Question
-
Hello,
I need some help about a crash I'm experiencing. The setup is as follows:
My C# application (the "App") calls a C# dll which is really a wrapper onto a C++ dll: App -> C# Wrapper -> Wrapped DLL. The Wrapped DLL internally uses exceptions but they are caught before propagating out of this Wrapped DLL. (Note that this Wrapped DLL has a C-only interface).
The C# Wrapper in some cases throws exceptions. I'm running into such a case in the App. In debug mode, right before the exception is thrown, the App crashes with the message "Windows has triggered a breakpoint in App. This may be due to a corruption of the heap, which indicates a bug in App or any of the DLLs it has loaded." I can click continue, in which case I get another message: "Unhandled exception at 0x775de753 in App: 0xC0000374: [a memory segment has been damaged].". If I click continue again, my catch block executes as expected.
In release mode, I don't have any visible crash (the App terminates as it is supposed to) but it seems my catch block is not executed.
The App and the C# wrapper are built with Visual Studio Professional 2012 with the .NET framework version 4.5. The Wrapped DLL is built with Visual Studio 2010.
My questions is the following: is it safe to throw exceptions across the boundaries of dlls written in C# and built with different versions of the .NET framework or Visual Studio?
thank you for your help.Tuesday, September 30, 2014 2:32 PM
Answers
-
It is safe to throw a .NET Exception across a DLL boundary. But I'm highly suspicious that your exception is arising from the C++ code secondary to a heap corruption. You need to analyze your C++ code for a memory corruption problem... e.g. mismatched malloc/free and new/delete pairs, buffer overruns, etc.
- Proposed as answer by davidbaxterbrowneMicrosoft employee Tuesday, September 30, 2014 5:24 PM
- Marked as answer by L B Wednesday, October 1, 2014 8:08 AM
Tuesday, September 30, 2014 3:31 PM
All replies
-
My questions is the following: is it safe to throw exceptions across the boundaries of dlls written in C# and built with different versions of the .NET framework or Visual Studio?
No there shouldn't be a problem.
Tuesday, September 30, 2014 3:07 PM -
It is safe to throw a .NET Exception across a DLL boundary. But I'm highly suspicious that your exception is arising from the C++ code secondary to a heap corruption. You need to analyze your C++ code for a memory corruption problem... e.g. mismatched malloc/free and new/delete pairs, buffer overruns, etc.
- Proposed as answer by davidbaxterbrowneMicrosoft employee Tuesday, September 30, 2014 5:24 PM
- Marked as answer by L B Wednesday, October 1, 2014 8:08 AM
Tuesday, September 30, 2014 3:31 PM -
Thank you very much for your quick answers!
As you said, exceptions weren't the problem. Instead, one of the C functions was returning a char buffer which needed to be marshaled with Marshal.PtrToStringAnsi.
Wednesday, October 1, 2014 8:11 AM