开发者

C++/CLI application crashing randomly on Release builds

开发者 https://www.devze.com 2023-02-03 02:43 出处:网络
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 Re

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.

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):

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

                return _me;
            };

where _me and _impl is

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

In the form on a button click I do just this:

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

Also I have a standard native entry point as usual DllMain. In the DEBUG build I use

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

at the beginning of DllMain, an in DEBUG build I also hav redefined new:

#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 my non-MFC apps to get a nice memory leaks.

The constructor of NativeClass is empty.

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

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).

The exception is the following:

************** Exception Text **************
System.TypeInitializationException: The type initializer for '<Module>' threw an exception. ---> System.AccessViolationException: Att开发者_开发百科empted 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, TypeInitializationException means there is something wrong with construction of objects) and why it is only in Release mode?


It doesn't have anything to do with the code snippet you posted, the code bombs before ManagedClassWrapper gets created. The <Module> class is a wrapper class around all of the non-ref class code that you wrote. It bombs when it tries to call the initializers of your unmanaged code. It's an AccessViolation, the usual way for unmanaged code to take a nose-dive.

To debug this you'll have to enable unmanaged debugging in your C# project. Project + Properties, Debug tab, tick "Enable unmanaged code debugging". Then Debug + Exceptions, tick the Thrown flag on "Win32 Exceptions". Run your code until the crash happens, the debugger will stop at the crash location. Should give you some idea where the bug is located. Use the normal debugging techniques that you're familiar with when working with unmanaged code. Good luck with it.

0

精彩评论

暂无评论...
验证码 换一张
取 消