Vivek Gupta

Vivek Gupta

  • NA
  • 1
  • 2.9k

Handle leaks when loading a C# dll through COM Interop

Oct 1 2013 3:06 PM

 Hi,

I am loading C# dll in my unmanaged code through COM Interop. Recently I noticed that handle count in my application is increasing due to this managed code. When remove the code to load C# dll, handle count become stable. Windbg is showing multiple open handles for following call stacks:

--------------------------------------

Handle = 0x0000000000000ee0 - OPEN
Thread ID = 0x0000000000000e90, Process ID = 0x00000000000011cc

0x000007ff06fc306a: ntdll!ZwCreateEvent+0x000000000000000a
0x000007fef4146a1f: vfbasics!AVrfpNtCreateEvent+0x00000000000000a3
0x000007ff040a271f: KERNELBASE!CreateEventW+0x0000000000000063
0x000007fef4146c30: vfbasics!AVrfpCreateEventW+0x00000000000000c4
0x000007fef8ef0aa7: clr!CLREventBase::CreateManualEvent+0x0000000000000028
0x000007fef8ef18d1: clr!Thread::AllocHandles+0x0000000000000051
0x000007fef8ef2681: clr!Thread::CreateNewOSThread+0x0000000000000091
0x000007fef8ef25a6: clr!Thread::CreateNewThread+0x00000000000000ae
0x000007fef8ef2e01: clr!ThreadpoolMgr::CreateUnimpersonatedThread+0x00000000000000c5
0x000007fef8dd44a8: clr!ThreadpoolMgr::CreateWorkerThread+0x0000000000000019
0x000007fef8dd4481: clr!ThreadpoolMgr::MaybeAddWorkingWorker+0x000000000000011d
0x000007fef8dd4b6c: clr!ManagedPerAppDomainTPCount::SetAppDomainRequestsActive+0x0000000000000024
0x000007fef8dd4c2a: clr!ThreadpoolMgr::SetAppDomainRequestsActive+0x000000000000002a
0x000007fef8dd4bba: clr!ThreadPoolNative::RequestWorkerThread+0x000000000000002f
--------------------------------------
Handle = 0x00000000000011b4 - OPEN
Thread ID = 0x0000000000000f84, Process ID = 0x00000000000011cc

0x000007ff06fc36fb: ntdll!ZwCreateThreadEx+0x000000000000000a
0x000007ff040c0d53: KERNELBASE!CreateRemoteThreadEx+0x0000000000000263
0x000007ff0414728b: KERNELBASE!CreateThread+0x000000000000003b
0x000007fef4149234: vfbasics!AVrfpCreateThread+0x00000000000000b0
0x000007fef8ef26b5: clr!Thread::CreateNewOSThread+0x00000000000000c5
0x000007fef8ef25a6: clr!Thread::CreateNewThread+0x00000000000000ae
0x000007fef8ef2e01: clr!ThreadpoolMgr::CreateUnimpersonatedThread+0x00000000000000c5
0x000007fef8dd44a8: clr!ThreadpoolMgr::CreateWorkerThread+0x0000000000000019
0x000007fef8dd4481: clr!ThreadpoolMgr::MaybeAddWorkingWorker+0x000000000000011d
0x000007fef8dd4b6c: clr!ManagedPerAppDomainTPCount::SetAppDomainRequestsActive+0x0000000000000024
0x000007fef8dd4c2a: clr!ThreadpoolMgr::SetAppDomainRequestsActive+0x000000000000002a
--------------------------------------

Here are few lines of the code that I am using to load the c# dll:

try {
m_var = ABC::IABCServerPtr(__uuidof(ABC::ABCServer)); 
  m_var->Initialize();
}

where m_var is ABC::IABCServerPtr . And call m_var->Initialize() prepares a hosting environment like web server.

I am very new to C# programming and by looking into this call stack I am not able to understand why CLR is allocating these handles. These handles are increasing with time. Can anyone please let me know why these handles are being created and how can we stop this handle leak ?

Thanks in advance !!!