我已经创建了一个C++/CLI混合动态链接库,我正在从C# Winforms应用程序中使用它。我仔细检查了构建配置,以确保我链接到调试模式下的调试库和发布模式下的非调试库。
目前,应用程序什么也不做,只是在托管包装器中创建一个本机类,就像这样(单例模式确保类的单个实例):
static ManagedClassWrapper ^ GetInstance(){
if(_me == nullptr){
_me = gcnew ManagedClassWrapper();
_me->_Impl = new NativeClass();
}
return _me;
};_me和_impl的位置
private:
NativeClass * _Impl;
static ManagedClassWrapper ^ _me = nullptr;在表单中单击一个按钮时,我只需执行以下操作:
private void button1_Click(object sender, EventArgs e)
{
ManagedClassWrapper mcw = ManagedClassWrapper.GetInstance();
}我还有一个标准的本机入口点,就像通常的DllMain一样。在调试版本中,我使用
_CrtSetReportHook( QaDMemManager::report );
_CrtSetDbgFlag((_CRTDBG_LEAK_CHECK_DF) | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));在DllMain的开始,我还重新定义了新的调试版本:
#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就像我通常对我的非MFC应用程序所做的那样,我得到了一个很好的内存泄漏。
NativeClass的构造函数为空。
在Debug版本中一切工作正常,我在本机代码中看到内存泄漏,没有崩溃。
但在Release build中,当我点击那个button1时,我的应用程序就会崩溃。这意味着:我可以启动我的应用程序的10个实例,9无论我点击多少次button1都可以正常工作,但每当我点击button1时,第10个实例都会崩溃(崩溃后,我在异常窗口中单击继续,因此我可以多次点击button1 )。
例外情况如下:
************** 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
----------------------------------------可能的问题是什么(据我所知,TypeInitializationException意味着对象的构造有问题),为什么它只处于发布模式?
发布于 2011-01-10 05:22:07
它与你发布的代码片段没有任何关系,也就是在ManagedClassWrapper创建之前的代码炸弹。<Module>类是您编写的所有非ref类代码的包装类。当它试图调用您的非托管代码的初始值设定项时,它会崩溃。这是一个AccessViolation,这是非托管代码一落千丈的常见方式。
要对此进行调试,必须在C#项目中启用非托管调试。“项目+属性”,“调试”选项卡,勾选“启用非托管代码调试”。然后调试+异常,在"Win32异常“上勾选抛出的标志。运行您的代码,直到发生崩溃,调试器将在崩溃位置停止。应该会让你知道bug在哪里。使用您在处理非托管代码时熟悉的常规调试技术。祝你好运。
https://stackoverflow.com/questions/4641715
复制相似问题