title: Why is my thread blocked by a critical section not being held by anything?
body: <p>I am having an issue with a critical section in C++.  I'm getting a hung window and when I dump the process I can see the thread waiting on a critical section:</p>
  
  <pre><code>  16  Id: b10.b88 Suspend: 1 Teb: 7ffae000 Unfrozen
  ChildEBP RetAddr  
  0470f158 7c90df3c ntdll!KiFastSystemCallRet
  0470f15c 7c91b22b ntdll!NtWaitForSingleObject+0xc
  0470f1e4 7c901046 ntdll!RtlpWaitForCriticalSection+0x132
  0470f1ec 0415647e ntdll!RtlEnterCriticalSection+0x46
  </code></pre>
  
  <p>The line data, etc, all indicates entry into a specific critical section.  The only problem is that no other threads appear to be holding this critical section open.  There's nothing indicated by Windbg's !locks command and dumping the critical section indicates it's not locked as can be seen by the null owner and the -1 LockCount in the structure below.</p>
  
  <pre><code>0:016&gt; dt _RTL_CRITICAL_SECTION 42c2318
  _RTL_CRITICAL_SECTION
     +0x000 DebugInfo        : 0x02c8b318 _RTL_CRITICAL_SECTION_DEBUG
     +0x004 LockCount        : -1
     +0x008 RecursionCount   : -1
     +0x00c OwningThread     : (null) 
     +0x010 LockSemaphore    : 0x00000340 
     +0x014 SpinCount        : 0
  
  0:016&gt; dt _RTL_CRITICAL_SECTION_DEBUG 2c8b318
  _RTL_CRITICAL_SECTION_DEBUG
     +0x000 Type             : 0
     +0x002 CreatorBackTraceIndex : 0x2911
     +0x004 CriticalSection  : 0x042c2318 _RTL_CRITICAL_SECTION
     +0x008 ProcessLocksList : _LIST_ENTRY [ 0x2c8b358 - 0x2c8b2e8 ]
     +0x010 EntryCount       : 1
     +0x014 ContentionCount  : 1
     +0x018 Flags            : 0xbaadf00d
     +0x01c CreatorBackTraceIndexHigh : 0xf00d
     +0x01e SpareWORD        : 0xbaad
  </code></pre>
  
  <p>How is this possible?  Even in a deadlock where another thread has not called LeaveCriticalSection I would expect to see the critical section itself marked as locked.  Does anyone have any debugging suggestions or possible fixes?</p>