MFC application that calls C# COM dll crashes intermittently with error code C0000005

  .net-runtime, c++, clr, com-interop, interop

We have a multi-threaded MFC application that crashes intermittently on Windows 10. The crash happens when calling a C# COM dll. We are currently on .Net 4.8

Eg: The crash happens while calling the following C# function from C++:

public void Update_xxx_Value(float fNew_xxx_Value);

the signature of the interface created by compiler is as follows:

virtual HRESULT __stdcall Update_xxx_Value (
        /*[in]*/ float fNew_xxx_Value ) = 0;

C++ side of the code goes:

IDatamanPtr     g_pDataMgmt = NULL;

Where _COM_SMARTPTR_TYPEDEF(IDataman, __uuidof(IDataman));

const float xxxValue = 10.99;
//...
g_pDataMgmt->Update_xxx_Value(10.99);

// this crash happens intermitently

There are more than 100 functions and this crash always happens while calling the COM dll’s function.

The crash dump analysis is as follows:

Exception code: C0000005
Exception Info: The thread tried to read from or write to a virtual address for which it does not have the appropriate access.

And in Event log Error "The process was terminated due to an internal error in the .Net Runtime at IP xxxxxxxxx with exit code 80131506"

In myApp.exe.4144.dmp the assembly instruction at clr!COMToCLRWorker+a4 in C:WindowsMicrosoft.NETFramework64v4.0.30319clr.dll from Microsoft Corporation has caused an access violation exception (0xC0000005) when trying to read from memory location 0x00000090 on thread 0

Number of objects ready for finalization: 17313

NET Call Stack

[[ComMethodFrame]]
DomainBoundILStubClass.IL_STUB_PInvoke()+68
[[InlinedCallFrame] (.wWinMainCRTStartup)] .wWinMainCRTStartup()
[[GCFrame]]

Full Call Stack

clr!COMToCLRWorker+a4
clr!GenericComCallStub+57
[[ComMethodFrame]]
myapp+3bcac1
myapp+11fe20
myapp+dacf9
mfc140u!CWnd::OnWndMsg+bdd
mfc140u!CWnd::WindowProc+3f
mfc140u!AfxCallWndProc+135
mfc140u!AfxWndProc+54
mfc140u!AfxWndProcBase+51
user32!UserCallWinProcCheckWow+266
user32!DispatchClientMessage+9c
user32!_fnDWORD+33
ntdll!KiUserCallbackDispatcherContinue
win32u!NtUserGetMessage+14
user32!GetMessageW+2d
mfc140u!AfxInternalPumpMessage+27
mfc140u!CWnd::RunModalLoop+108
mfc140u!CWnd::CreateRunDlgIndirect+3f
mfc140u!CDialog::DoModal+18b
myapp+36b5e9
myapp+3711f6
myapp+3728f6
mfc140u!AfxWinMain+84
myapp+4dfe1e
DomainBoundILStubClass.IL_STUB_PInvoke()+68
[[InlinedCallFrame] (<Module>.wWinMainCRTStartup)] <Module>.wWinMainCRTStartup()
clr!CallDescrWorkerInternal+83
clr!CallDescrWorkerWithHandler+4e
clr!MethodDescCallSite::CallTargetWorker+102
clr!RunMain+266
[[GCFrame]]
clr!Assembly::ExecuteMainMethod+b7
clr!SystemDomain::ExecuteMainMethod+643
clr!ExecuteEXE+3f
clr!_CorExeMainInternal+b2
clr!CorExeMain+14
mscoreei!CorExeMain+112
mscoree!CorExeMain_Exported+6c
kernel32!BaseThreadInitThunk+14
ntdll!RtlUserThreadStart+21

Finalizer Thread

ntdll!NtWaitForSingleObject+14
KERNELBASE!WaitForSingleObjectEx+93
clr!CLREventWaitHelper2+3c
clr!CLREventWaitHelper+1f
clr!CLREventBase::WaitEx+7c
clr!FinalizerThread::WaitForFinalizerEvent+44
clr!FinalizerThread::FinalizerThreadWorker+54
clr!ManagedThreadBase_DispatchInner+39
clr!ManagedThreadBase_DispatchMiddle+6c
clr!ManagedThreadBase_DispatchOuter+75
[[DebuggerU2MCatchHandlerFrame]]
clr!FinalizerThread::FinalizerThreadStart+126
clr!Thread::intermediateThreadProc+86
kernel32!BaseThreadInitThunk+14
ntdll!RtlUserThreadStart+21

Thread 15 – System ID 6684

Entry point clr!Thread::intermediateThreadProc
Create time 29-11-2021 3.24.19 PM
Time spent in user mode 0 Days 00:00:00.000
Time spent in kernel mode 0 Days 00:00:00.015

This thread is not fully resolved and may or may not be a problem. Further analysis of these threads may be required.

.NET Call Stack

System_ni!DomainBoundILStubClass.IL_STUB_PInvoke(Int32, IntPtr, Int32, Int32, Int32)+8f 
[[InlinedCallFrame] (Microsoft.Win32.UnsafeNativeMethods.MsgWaitForMultipleObjectsEx)] Microsoft.Win32.UnsafeNativeMethods.MsgWaitForMultipleObjectsEx(Int32, IntPtr, Int32, Int32, Int32) 
System_ni!Microsoft.Win32.SystemEvents.WindowThreadProc()+a1 
mscorlib_ni!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)+172 
mscorlib_ni!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)+15 
mscorlib_ni!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)+55 
mscorlib_ni!System.Threading.ThreadHelper.ThreadStart()+55 
[[GCFrame]] 
[[DebuggerU2MCatchHandlerFrame]] 

Full Call Stack

win32u!NtUserMsgWaitForMultipleObjectsEx+14 
user32!RealMsgWaitForMultipleObjectsEx+1d 
System_ni!DomainBoundILStubClass.IL_STUB_PInvoke(Int32, IntPtr, Int32, Int32, Int32)+8f 
[[InlinedCallFrame] (Microsoft.Win32.UnsafeNativeMethods.MsgWaitForMultipleObjectsEx)] Microsoft.Win32.UnsafeNativeMethods.MsgWaitForMultipleObjectsEx(Int32, IntPtr, Int32, Int32, Int32) 
System_ni!Microsoft.Win32.SystemEvents.WindowThreadProc()+a1 
mscorlib_ni!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)+172 
mscorlib_ni!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)+15 
mscorlib_ni!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)+55 
mscorlib_ni!System.Threading.ThreadHelper.ThreadStart()+55 
clr!CallDescrWorkerInternal+83 
clr!CallDescrWorkerWithHandler+4e 
clr!MethodDescCallSite::CallTargetWorker+102 
clr!ThreadNative::KickOffThread_Worker+fffff01f 
[[GCFrame]] 
clr!ManagedThreadBase_DispatchInner+40 
clr!ManagedThreadBase_DispatchMiddle+6c 
clr!ManagedThreadBase_DispatchOuter+4c 
[[DebuggerU2MCatchHandlerFrame]] 
clr!ManagedThreadBase_FullTransitionWithAD+2f 
clr!ThreadNative::KickOffThread+e6 
clr!Thread::intermediateThreadProc+8b 
kernel32!BaseThreadInitThunk+14 
ntdll!RtlUserThreadStart+21 

We went through multiple forums where similar issues have been reported. Solutions tried so far:

  1. Upgraded .Net run time to latest 4.8

  2. Updated OS to latest Windows 10.

  3. Added a guard around the pointer used to call COM APIs as below:

    xx::IDatamanPtr DataMgmt()
    {
    const std::lock_guardstd::mutex lock(g_DataMgmt_Mutex);
    return g_pDataMgmt;
    }

  4. Tried to disable concurrent gc by adding an app.config file for the
    C# dlls. But this may not be a right approach. Need help in
    disabling GC in an MFC application that calls COM dlls

This one seems tough one to crack

Source: Windows Questions C++

LEAVE A COMMENT