Note that there are some explanatory texts on larger screens.

plurals
  1. POC++/CLI application crashing randomly on Release builds
    text
    copied!<p>I have created a C++/CLI mixed DLL which I am using from C# Winforms application. I have carefuly checked Build config to be sure that I am linking to debug libs in Debug mode and non-debug libs in Release.</p> <p>For now the application is doing nothing, just creating a native class in a managed wrapper like this (singleton pattern to ensure a single instance of the class):</p> <pre><code>static ManagedClassWrapper ^ GetInstance(){ if(_me == nullptr){ _me = gcnew ManagedClassWrapper(); _me-&gt;_Impl = new NativeClass(); } return _me; }; </code></pre> <p>where _me and _impl is</p> <pre><code>private: NativeClass * _Impl; static ManagedClassWrapper ^ _me = nullptr; </code></pre> <p>In the form on a button click I do just this:</p> <pre><code>private void button1_Click(object sender, EventArgs e) { ManagedClassWrapper mcw = ManagedClassWrapper.GetInstance(); } </code></pre> <p>Also I have a standard native entry point as usual DllMain. In the DEBUG build I use </p> <pre><code>_CrtSetReportHook( QaDMemManager::report ); _CrtSetDbgFlag((_CRTDBG_LEAK_CHECK_DF) | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG)); </code></pre> <p>at the beginning of DllMain, an in DEBUG build I also hav redefined new:</p> <pre><code>#ifdef _DEBUG #define _CRTDBG_MAP_ALLOC #define LOG_LEVEL Logger::NOTICE #include &lt;stdlib.h&gt; #include &lt;crtdbg.h&gt; #pragma warning(disable:4291) #define new new(_NORMAL_BLOCK,__FILE__, __LINE__) #else #define LOG_LEVEL Logger::INFO #endif </code></pre> <p>as I usually do for my non-MFC apps to get a nice memory leaks.</p> <p>The constructor of NativeClass is empty.</p> <p>Everything works fine in Debug builds, I see memory leaks in native code, no crashes.</p> <p>But in Release build one time out of 10 my app just crashes when I click that button1. It means: I can launch 10 instances of my app, 9 will work ok no matter how many times I click the button1, but the 10th will crash every time I click the button1 (after the crash I click Continue in the exception window and so I can click button1 many times).</p> <p>The exception is the following:</p> <pre><code>************** Exception Text ************** System.TypeInitializationException: The type initializer for '&lt;Module&gt;' threw an exception. ---&gt; 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 &lt;CrtImplementationDetails&gt;.LanguageSupport.InitializeNative(LanguageSupport* ) at &lt;CrtImplementationDetails&gt;.LanguageSupport._Initialize(LanguageSupport* ) at &lt;CrtImplementationDetails&gt;.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&amp; m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message&amp; m) at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m) at System.Windows.Forms.Button.WndProc(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; 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 ---------------------------------------- </code></pre> <p>What could be the problem (as I understand, TypeInitializationException means there is something wrong with construction of objects) and why it is only in Release mode?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload