C#中的堆错误

时间:2012-07-03 07:48:30

标签: c# c++ dll heap

我有一个C#程序来测试C ++ / CLI程序集(这个程序集是本机C ++ DLL的包装器),我收到以下错误消息:

Unhandled exception at 0x50f8fd85 (msvcr90d.dll) in TestKeyManagerApp.exe: 0xC0000005:     Access violation writing location 0x00000000.

调试模式下的最后位置:dbgheap.c(Visual Studio 2008输出)

if (pHead == NULL)
{
    *errno_tmp = ENOMEM; <----- ACCESS VIOLATION
    RTCCALLBACK(_RTC_FuncCheckSet_hook,(1));
}

我创建了一次汇编类的对象(我的C#程序的私有成员),在构造函数中我加载了本机C ++ dll,现在如果我第二次使用汇编对象(第二次调用testfunction)我得到了这个错误消息!

调用堆栈的输出:

msvcr90d.dll!_heap_alloc_dbg_impl(unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x50ec24a8, int nLine=588, int * errno_tmp=0x00000000)  Line 431 + 0x3 bytes C++

msvcr90d.dll!_nh_malloc_dbg_impl(unsigned int nSize=532, int nhFlag=0, int nBlockUse=2, const char * szFileName=0x50ec24a8, int nLine=588, int * errno_tmp=0x00000000)  Line 239 + 0x19 bytes   C++

msvcr90d.dll!_calloc_dbg_impl(unsigned int nNum=1, unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x50ec24a8, int nLine=588, int * errno_tmp=0x00000000)  Line 593 + 0x20 bytes   C++

msvcr90d.dll!_getptd_noexit()  Line 588 + 0x1a bytes    C

msvcr90d.dll!_errno()  Line 281 + 0x5 bytes C

msvcr90d.dll!_calloc_dbg(unsigned int nNum=1, unsigned int nSize=532, int nBlockUse=2, const char * szFileName=0x50ec20a4, int nLine=373)  Line 646 + 0x11 bytes    C++

msvcr90d.dll!__CRTDLL_INIT(void * hDllHandle=0x50ec0000, unsigned long dwReason=2, void * lpreserved=0x00000000)  Line 373 + 0x18 bytes C

msvcr90d.dll!_CRTDLL_INIT(void * hDllHandle=0x50ec0000, unsigned long dwReason=2, void * lpreserved=0x00000000)  Line 214 + 0x11 bytes  C

ntdll.dll!77c68968()    
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]
ntdll.dll!77c73820()    
ntdll.dll!77c735bc()    

我希望你能给我提示或提示吗?

THX!

2 个答案:

答案 0 :(得分:1)

检查调用堆栈,特别是非托管堆栈。为此,您必须启用非托管代码调试。 这可能是伪造删除或悬挂/无效指针访问。通过最小化本机DLL调用来缩小问题。

答案 1 :(得分:0)

错误的潜在原因:

  • 指针errno_tmp可能指向已删除的内存,或指向数组边界外的点(-ve index或index&gt; = length)。

  • 使用了无效的P\Invoke,或已加载的程序集已free