访问违例在MFC dll(包装在C ++ / CLI)从C#程序启动 [英] Access violation in MFC dll (wrapped in C++/CLI) started from C# program

查看:164
本文介绍了访问违例在MFC dll(包装在C ++ / CLI)从C#程序启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为mfc dll(C ++)编写了托管C ++ / CLI包装,并且在第二次调用dll后有一些访问冲突!



Wrapper

  // in .h 
typedef CKeyManagerServerApp *(* KeyManagerInstance)(CCommonUtils *);

ManagedKeyInterface :: ManagedKeyInterface()
{
HINSTANCE m_keyManagerLib = LoadLibrary(pathToDll);

KeyManagerInstance _createInstance =(KeyManagerInstance)GetProcAddress(m_keyManagerLib,GetInstance);

//从管理的读取器接口获取本地读取器接口
CCommonUtils * nativeReaderInterface = static_cast< CCommonUtils *>(readerInterface-> nativeReaderInterface.ToPointer());

CKeyManagerServerApp * m_keyManagerApp =(_createInstance)(nativeReaderInterface);
}

ManagedKeyInterface ::〜ManagedKeyInterface()
{
try
{
DestroyKeyManagerInstance _destroyInstance =(DestroyKeyManagerInstance)GetProcAddress(m_keyManagerLib, DestroyInstance);
(_destroyInstance)(m_keyManagerApp);

FreeLibrary(m_keyManagerLib);
}
catch(System :: Exception ^ e)
{
FreeLibrary(m_keyManagerLib);
}
}

NATIVE MFC CLASS / p>

  externC_declspec(dllexport)CKeyManagerServerApp * GetInstance(CCommonUtils * readerInterface)
{
AFX_MANAGE_STATE AfxGetStaticModuleState());

return new CKeyManagerServerApp(readerInterface);
}

externC_declspec(dllexport)void DestroyInstance(CKeyManagerServerApp * ptr)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());

delete ptr;
}

//构造函数
CKeyManagerServerApp :: CKeyManagerServerApp(CCommonUtils * readerInterface)
{
m_log = new Logging(loggingFilePath); //< ---第二次调用时的错误


//用于通信的读取器接口对象
m_readerComm = new ReaderCommunication(readerInterface,m_log);

m_smartmaskcmds = new CSmartMaskCmds(m_readerComm,m_log);

readerInterface = NULL;
}

// destructor
CKeyManagerServerApp ::〜CKeyManagerServerApp()
{
// destruct objects
delete m_smartmaskcmds;
delete m_readerComm;
delete m_log;在ReaderCommunication和CSmartMaskCmds constr中的
}

对象只会被赋值!



在C#程序的第一次运行时(加载引用的包装器)一切正常,但是当我再次启动它,我得到: / p>

TestKeyManagerApp.exe中0x76f85b57处的第一次例外:0xC0000005:访问冲突读取位置0xdddddddd。
在TestKeyManagerApp.exe的0x75169617的第一次例外:Microsoft C ++异常:CMemoryException在内存位置0x0024e820 ..



当我调用m_log = new Logging(loggingFilePath )



似乎析构函数无法正常工作!



任何想法! >

谢谢!

解决方案

当您看到 0xdddddddd ,表示一些指针被删除(VC将在调试版本中设置该值,以帮助您识别这些情况)。你不告诉我们什么是 loggingFilePath 以及如何实现日志,但我的猜测是 loggingFilePath 在某一时刻被删除,并且 Logging 尝试在构造函数(或初始化列表)中访问其值或虚函数。 p>

这也可以解释析构函数中的崩溃 - 你正在删除 m_log ,这可能包含一个非法的指针从 loggingFilePath 。当你再次使用它,你会得到同样的崩溃。


I have written an managed C++/CLI wrapper for mfc dll (C++) and have some access violations after second call of dll!

Wrapper

// in .h
typedef CKeyManagerServerApp* (*KeyManagerInstance)(CCommonUtils *);

ManagedKeyInterface::ManagedKeyInterface()
{
    HINSTANCE m_keyManagerLib = LoadLibrary("pathToDll");

    KeyManagerInstance _createInstance = (KeyManagerInstance)GetProcAddress(m_keyManagerLib, "GetInstance");

    // get native reader interface from managed reader interface
    CCommonUtils *nativeReaderInterface = static_cast<CCommonUtils*>(readerInterface->nativeReaderInterface.ToPointer());

    CKeyManagerServerApp *m_keyManagerApp = (_createInstance)(nativeReaderInterface );
}

ManagedKeyInterface::~ManagedKeyInterface()
{
    try
{
    DestroyKeyManagerInstance _destroyInstance = (DestroyKeyManagerInstance)GetProcAddress(m_keyManagerLib, "DestroyInstance");
    (_destroyInstance)(m_keyManagerApp);

    FreeLibrary(m_keyManagerLib);           
}
    catch(System::Exception ^e)
    {
        FreeLibrary(m_keyManagerLib);
    }
}

NATIVE MFC CLASS

extern "C" _declspec(dllexport) CKeyManagerServerApp* GetInstance(CCommonUtils *readerInterface)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    return new CKeyManagerServerApp(readerInterface);
}

extern "C" _declspec(dllexport) void DestroyInstance(CKeyManagerServerApp *ptr)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    delete ptr;
}

// constructor
CKeyManagerServerApp::CKeyManagerServerApp(CCommonUtils *readerInterface)   
{
    m_log = new Logging(loggingFilePath); // <--- ERROR at second call


    // reader interface object for communication 
    m_readerComm = new ReaderCommunication(readerInterface, m_log); 

    m_smartmaskcmds = new CSmartMaskCmds(m_readerComm, m_log);

    readerInterface = NULL;
}

// destructor
CKeyManagerServerApp::~CKeyManagerServerApp()
{
    // destruct objects     
    delete m_smartmaskcmds; 
    delete m_readerComm;    
    delete m_log;   
}

in ReaderCommunication and CSmartMaskCmds constr. the object will only assigned!

At first runtime of the C# program (loaded the wrapper with add reference) everything works fine, but when I start it again I get:

First-chance exception at 0x76f85b57 in TestKeyManagerApp.exe: 0xC0000005: Access violation reading location 0xdddddddd. First-chance exception at 0x75169617 in TestKeyManagerApp.exe: Microsoft C++ exception: CMemoryException at memory location 0x0024e820..

when I call m_log = new Logging(loggingFilePath)

It seems the destructor does not work right!?

Any ideas!!??

Thank you!

解决方案

When you see the value 0xdddddddd, it means that some pointer was deleted (VC will set that value on debug builds to help you recognize these cases). You don't tell us what's loggingFilePath and how Logging is implemented, but my guess is that loggingFilePath is deleted at some point, and Logging tries to access its value or a virtual function in the constructor (or initialization list).

This could also explain the crash in the destructor - you're deleting m_log, which probably holds an illegal pointer it got from loggingFilePath. When you try to use it again, you get the same crash.

这篇关于访问违例在MFC dll(包装在C ++ / CLI)从C#程序启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆