Question Handle leaks when loading a C# dll through COM Interop

Vivek03.gupta

New member
Joined
Oct 1, 2013
Messages
1
Programming Experience
5-10
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
--------------------------------------
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 !!!
 
I've never used a managed assembly in an unmanaged application like that so I don't know all the details. What I do know that all managed applications have threads available for background work via the ThreadPool class, so it makes sense that a managed assembly in an unmanaged application does too. Those ThreadPool threads are used by the BackgroundWorker class, the Task class, all asynchronous methods and can also be used directly through the ThreadPool by you. I think that the ThreadPool creates four threads by default and then more if they are required up to a prescribed maximum. Given the mention of the ThreadPool in that information you posted, I would assume that that's all it is.
 
Back
Top Bottom