1

The situation:

In my C# solution (VS 2013) I added a C project. The C project contains a function that is declared in C# with:

[DllImport("My_C_Dll.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void My_C_Function(IntPtr OutputArray, int Input);

The function is called with this code:

var OutputArray = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(UInt16)) * OutputLength);

My_C_Function(OutputArray, Input);

Int16[] ManagedOutputArray= new Int16[OutputLength];
Marshal.Copy(OutputArray, ManagedOutputArray, 0, OutputLength);
Marshal.FreeHGlobal(OutputArray);

The issue:

When "My_C_Function" is called the application hangs (actually is the Visual Studio Hosting process "vshost32-clr2.exe" that hangs) when ALL the following conditions are satisfied:

  • when I run WITH the debugger attached (starting application from Visual Studio)

  • when I DISABLE the "Enable native code debugging" in the Project properties

  • when I ENABLE the "Enable the Visual Studio hosting process"

My idea

It seems that "vshost32-clr2.exe" stops working because it is not able to execute the C extern function if the "native code debugging" is disabled. It seems that it needs some feature that is given only when "native code debugging" is enabled.

My issue seems the same of AccessViolationException goes away when native code debugging is enabled but no one has already answered why this happens.

My question WHY? For another project I already did a different solution that calls a different C extern functions without such issue (I can leave the "hosting process" enabled and the "native code debugging" disabled without any problem) but I cannot find the difference between the two solutions that leads to this error.

Any help would be greatly appreciated!

Community
  • 1
  • 1
Homer1982
  • 441
  • 4
  • 16
  • Enable JIT debugging or the Exception windows, do you get any exception messages? For mix mode debugging, we need to enable the native code debugging in the C# project. If you disable the host process, and enable the native code debugging, do you debug your app normally? – Jack Zhai Aug 03 '16 at 10:40
  • @Jack Zhai if I disable the hosting and leave the native code debugging enabled it works. Only if I use hosting without native code debugging the hosting process hangs. I set Visual Studio to stop at any possible exception (thrown and user-unhandled) but the debugger does not stop. Running the code step by step I noticed that that it hangs when I call "My_C_Function" – Homer1982 Aug 04 '16 at 12:22
  • Do you get any messages in the output window? – Jack Zhai Aug 05 '16 at 03:54
  • @Jack Zhai After dll and pdb loading I just read: The thread 0x8e6c has exited with code 259 (0x103). The thread 0x995c has exited with code 259 (0x103). The thread 0x92a8 has exited with code 259 (0x103). The thread 0x99b8 has exited with code 259 (0x103). The thread 0x8438 has exited with code 259 (0x103). 05/08/2016 05:58:51 Specific TD 0004 – Homer1982 Aug 05 '16 at 06:02
  • Please share me a sample, I will debug it in my side. Since other project has no this issue, the project code would be a path like this case: http://stackoverflow.com/questions/36153362/any-idea-what-can-cause-vshost32-exe-has-stopped-working-in-visual-studio-2013 I found it before. – Jack Zhai Aug 05 '16 at 07:00

1 Answers1

0

By disabling code optimization in c++ project properties->C/C++->Optimization I've been able to find an unhandled exception in c++ code. After fixing some small memory leaks I managed to make it working even with hosting process active and native code debugging disabled.

Homer1982
  • 441
  • 4
  • 16