title: C# PerformanceCounter Class causes Handle Leak
body: <p>I have an application developed with .NET 4.0. This application keeps track on some Custom Performance Counter and display to the user. Recently i found that there's handle leak in the application. The 2 types of handles are Mutant and PcwObject. </p>
  
  <p>I followed this page (<a href="http://blogs.technet.com/b/yongrhee/archive/2011/12/19/how-to-troubleshoot-a-handle-leak.aspx" rel="nofollow">http://blogs.technet.com/b/yongrhee/archive/2011/12/19/how-to-troubleshoot-a-handle-leak.aspx</a>) and got the following stack trace:</p>
  
  <pre><code>Handle = 0x0000000000003760 - OPEN
  Thread ID = 0x00000000000073d0, Process ID = 0x0000000000005fdc
  
  0x0000000077c41cea: ntdll!NtCreateMutant+0x000000000000000a
  0x000007fefde08bf7: KERNELBASE!CreateMutexExW+0x000000000000004f
  0x000007fefde14460: KERNELBASE!CreateMutexExA+0x0000000000000038
  0x000007feff6bbcf6: ADVAPI32!PerflibciOpenLocalQueryHandle+0x0000000000000116
  0x000007feff6a5a86: ADVAPI32!PerflibciQueryV2Provider+0x000000000000020d
  0x000007feff68926d: ADVAPI32!QueryExtensibleData+0x00000000000004a2
  0x000007feff6898e4: ADVAPI32!alloca_probe+0x00000000000051b2
  0x00000000779d4087: KERNEL32!TlsGetValue+0x000000000000fbb8
  0x00000000779e4b52: KERNEL32!RegQueryValueExW+0x00000000000000f2
  0x000007feff68c2ed: ADVAPI32!RegQueryValueExWStub+0x000000000000001d
  0x000007fef99b17c7: clr!DoNDirectCall__PatchGetThreadCall+0x000000000000007b
  0x000007fef8a38422: mscorlib_ni+0x0000000000428422
  0x000007fef89948f1: mscorlib_ni+0x00000000003848f1
  0x000007fef899392e: mscorlib_ni+0x000000000038392e
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Handle = 0x0000000000003998 - OPEN
  Thread ID = 0x0000000000002808, Process ID = 0x0000000000005fdc
  
  0x0000000077c4138a: ntdll!NtDeviceIoControlFile+0x000000000000000a
  0x000007fefce214a3: pcwum!PcwpSendIoctl+0x00000000000000f3
  0x000007fefce21962: pcwum!PcwCreateNotifier+0x000000000000003e
  0x000007feff6abf53: ADVAPI32!PerfpCreateProvider+0x00000000000000d3
  0x000007feff6ddb77: ADVAPI32!PerflibciLocalValidateCounters+0x0000000000000167
  0x000007feff6a5ced: ADVAPI32!PerflibciQueryV2Provider+0x0000000000000478
  0x000007feff68926d: ADVAPI32!QueryExtensibleData+0x00000000000004a2
  0x000007feff6898e4: ADVAPI32!alloca_probe+0x00000000000051b2
  0x00000000779d4087: KERNEL32!TlsGetValue+0x000000000000fbb8
  </code></pre>
  
  <p>I also opened Process Explorer to monitor the handle state. According to my observation, the above 2 handles (3760 and 3998) keep alive for over half an hour and not yet destroyed. The handles count is increased by ~1000 within 2 hours. half of them are Mutant and the other half are PcwObject.</p>
  
  <p>I suspect it is related to PerformanceCounter coz i know the PerformanceCounter Class grep data from Registry and i find PerflibciQuery and RegQueryValue in the stack trace.</p>
  
  <p>I've search through the Internet but seems no luck. Does anyone have any idea about this?
  Thanks</p>
  
  <p><strong>Additional Information</strong></p>
  
  <p>I tested those Performance Counters one by one and find that these handles were leaked when getting this counter : <code>PerformanceCounter("HTTP Service Request Queues", "CurrentQueueSize", "ABC")</code> </p>
  
  <p>My Code is like this : </p>
  
  <pre><code>private PerformanceCounter counter;
  
  private void Detect()
  {
      /*
      Do sth
      */
  
      try
      {
          if (null == counter) counter = new PerformanceCounter("HTTP Service Request Queues", "CurrentQueueSize", "ABC");
          long rawValue = counter.RawValue;
          if (0 &lt; rawValue)
              WriteLog("ABC CurrentQueueSize: {0}", rawValue);
      }
      catch(Exception e)
      {
          WriteLog("Fail to get ABC counter. {0}", e);
      }
  
  }
  </code></pre>
  
  <p><code>counter</code> is a member variable and I'm very sure that it is disposed when this class is destroyed. So i don't know why it leaks the handles.</p>