Heap error in C#

2019-08-02 12:48发布

问题:

I have a C# program to test a C++/CLI assembly (this assembly is a wrapper to a native C++ dll) and I'm getting following error message:

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

last position in debug mode: dbgheap.c (Visual studio 2008 output)

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

I create an object of the assembly class once (private member of my C# program), in constructor I load the native C++ dll and now if I use the assembly object second time (second call of a testfunction) I got this error messages!

output of Call Stack:

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

I hope you will have hints or tips for me?

Thx!

回答1:

Check the call stack, specially the unmanaged one. For this you must enable Unmanaged-code Debugging. This is probably bogus delete or danging/invalid pointer access. Narrow down the problem by minimizing the native DLL call.



回答2:

Potential reasons for the error:

  • pointer errno_tmp may be pointing to deleted memory, or points outsides the array bounds (-ve index or index >= length).

  • An invalid P\Invoke is used, or the loaded assembly has been free



标签: c# c++ dll heap