C ++ / CLI application accidentally crashes into Release builds version

I created a mixed C ++ / CLI DLL that I use from a C # Winforms application. I carefully checked the Build config to make sure that I am communicating with debug libs in debug mode and non-debug libs in Release.

Currently, the application does nothing, simply creating its own class in a managed shell like this (singleton pattern to provide a single instance of the class):

static ManagedClassWrapper ^ GetInstance(){ if(_me == nullptr){ _me = gcnew ManagedClassWrapper(); _me->_Impl = new NativeClass(); } return _me; }; 

where _me and _impl

 private: NativeClass * _Impl; static ManagedClassWrapper ^ _me = nullptr; 

In the form of a push of a button, I do exactly this:

 private void button1_Click(object sender, EventArgs e) { ManagedClassWrapper mcw = ManagedClassWrapper.GetInstance(); } 

In addition, I have a standard built-in entry point, as usual with DllMain. In DEBUG assembly I use

 _CrtSetReportHook( QaDMemManager::report ); _CrtSetDbgFlag((_CRTDBG_LEAK_CHECK_DF) | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)); 

at the beginning of DllMain, in the DEBUG assembly, I also redefined the new one:

 #ifdef _DEBUG #define _CRTDBG_MAP_ALLOC #define LOG_LEVEL Logger::NOTICE #include <stdlib.h> #include <crtdbg.h> #pragma warning(disable:4291) #define new new(_NORMAL_BLOCK,__FILE__, __LINE__) #else #define LOG_LEVEL Logger::INFO #endif 

as I usually do for non-MFC applications to get memory leaks.

NativeClass constructor is empty.

Everything works fine in Debug builds, I see memory leaks in the native code, no crashes.

But in Release, once out of 10, my application just crashes when I click this button1. This means: I can start 10 instances of my application, 9 will work fine, regardless of how many times I press button1, but the 10th will crash every time I press button1 (after a failure, I click Continue in the exception window and so I can press button1 many times).

An exception is the following:

 ************** Exception Text ************** System.TypeInitializationException: The type initializer for '<Module>' threw an exception. ---> System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at _initterm((fnptr)* pfbegin, (fnptr)* pfend) at <CrtImplementationDetails>.LanguageSupport.InitializeNative(LanguageSupport* ) at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* ) at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* ) at .cctor() --- End of inner exception stack trace --- at TestAudioInOut.TestForm.button1_Click(Object sender, EventArgs e) at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) ************** Loaded Assemblies ************** mscorlib Assembly Version: 4.0.0.0 Win32 Version: 4.0.30319.1 (RTMRel.030319-0100) CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll ---------------------------------------- TestAudioInOut Assembly Version: 1.0.0.0 Win32 Version: 1.0.0.0 CodeBase: file:///V:/Test/bin/Release/Test.exe ---------------------------------------- System.Windows.Forms Assembly Version: 4.0.0.0 Win32 Version: 4.0.30319.1 built by: RTMRel CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll ---------------------------------------- System.Drawing Assembly Version: 4.0.0.0 Win32 Version: 4.0.30319.1 built by: RTMRel CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll ---------------------------------------- System Assembly Version: 4.0.0.0 Win32 Version: 4.0.30319.1 built by: RTMRel CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll ---------------------------------------- Mixed.DLL Assembly Version: 1.0.4026.39493 Win32 Version: CodeBase: file:///V:/Test/bin/Release/Mixed.DLL ---------------------------------------- 

What could be the problem (as I understand it, TypeInitializationException means that something is wrong with the construction of objects) and why is this only in Release mode?

+4
source share
1 answer

This has nothing to do with the code snippet you posted, code bombs, before creating the ManagedClassWrapper. The <Module> class is a wrapper class around all the non-ref class code you wrote. It bombs when it tries to call the initializers of your unmanaged code. This is AccessViolation, the usual way for unmanaged code to accept a nose dive.

To debug this, you will have to include unmanaged debugging in your C # project. Project + Properties, Debug tab, check "Enable unmanaged code debugging." Then Debug + Exceptions, check the Drop box in Win32 Exceptions. Run the code until the crash, the debugger will stop at the crash site. Should give you an idea of ​​where the error is. Use the usual debugging methods that you are familiar with when working with unmanaged code. Good luck to you.

+5
source

Source: https://habr.com/ru/post/1334843/


All Articles