{"id":28763,"date":"2006-12-11T10:00:00","date_gmt":"2006-12-11T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2006\/12\/11\/what-does-an-invalid-handle-exception-in-leavecriticalsection-mean\/"},"modified":"2006-12-11T10:00:00","modified_gmt":"2006-12-11T10:00:00","slug":"what-does-an-invalid-handle-exception-in-leavecriticalsection-mean","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20061211-00\/?p=28763","title":{"rendered":"What does an invalid handle exception in LeaveCriticalSection mean?"},"content":{"rendered":"<p>\nInternally, a critical section is a bunch of counters and flags,\nand possibly an event.\n(Note that the internal structure of a critical section is subject\nto change at any time&mdash;in fact, it changed between\nWindows&nbsp;XP and Windows&nbsp;2003.\nThe information provided here is therefore intended for troubleshooting\nand debugging purposes and not for production use.)\nAs long as there is no contention, the counters and flags are\nsufficient because nobody has had to wait for the critical section\n(and therefore nobody had to be woken up when the critical section\nbecame available).\n<\/p>\n<p>\nIf a thread needs to be blocked because the critical section it wants\nis already owned by another thread,\nthe kernel creates an event for the critical section\n(if there isn&#8217;t one already) and waits on it.\nWhen the owner of the critical section finally releases it,\nthe event is signaled, thereby alerting all the waiters that the\ncritical section is now available and they should try to enter it\nagain.\n(If there is more than one waiter, then only one will actually\nenter the critical section and the others will return to the wait\nloop.)\n<\/p>\n<p>\nIf you get an invalid handle exception in\n<code>LeaveCriticalSection<\/code>,\nit means that the critical section code thought that there\nwere other threads waiting for the critical section to become\navailable, so it tried to signal the event, but the event handle\nwas no good.\n<\/p>\n<p>\nNow you get to use your brain to come up with reasons why this might be.\n<\/p>\n<p>\nOne possibility is that the critical section has been corrupted,\nand the memory that normally holds the event handle has been\noverwritten with some other value that happens not to be a\nvalid handle.\n<\/p>\n<p>\nAnother possibility is that some other piece of code passed\nan uninitialized variable to the <code>CloseHandle<\/code>\nfunction and ended up closing the critical section&#8217;s handle\nby mistake.\nThis can also happen if some other piece of code has a double-close\nbug, and the handle (now closed) just happened to be reused as the\ncritical section&#8217;s event handle.\nWhen the buggy code closes the handle the second time by mistake,\nit ends up closing the critical section&#8217;s handle instead.\n<\/p>\n<p>\nOf course, the problem might be that the critical section is not\nvalid because it was never initialized in the first place.\nThe values in the fields are just uninitialized garbage,\nand when you try to leave this uninitialized critical section,\nthat garbage gets used as an event handle, raising the invalid\nhandle exception.\n<\/p>\n<p>\nThen again, the problem might be that the critical section is\nnot valid because it has already been destroyed.\nFor example, one thread might have code that goes like this:\n<\/p>\n<pre>\nEnterCriticalSection(&amp;cs);\n... do stuff...\nLeaveCriticalSection(&amp;cs);\n<\/pre>\n<p>\nWhile that thread is busy doing stuff,\nanother thread calls\n<code>DeleteCriticalSection(&amp;cs)<\/code>.\nThis destroys the critical section while another thread\nwas still using it.\nEventually that thread finishes doing its stuff and calls\n<code>LeaveCriticalSection<\/code>,\nwhich raises the invalid handle exception because the\n<code>DeleteCriticalSection<\/code> already closed the handle.\n<\/p>\n<p>\nAll of these are possible reasons for an invalid handle\nexception in <code>LeaveCriticalSection<\/code>.\nTo determine which one you&#8217;re running into will require more\ndebugging, but at least now you know what to be looking for.\n<\/p>\n<p>\nPostscript:\nOne of my colleagues from the kernel team points out that\nthe Locks and Handles checks in\n<a HREF=\"http:\/\/www.microsoft.com\/technet\/prodtechnol\/windows\/appcompatibility\/appverifier.mspx\">\nApplication Verifier<\/a> are great\nfor debugging issues like this.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Internally, a critical section is a bunch of counters and flags, and possibly an event. (Note that the internal structure of a critical section is subject to change at any time&mdash;in fact, it changed between Windows&nbsp;XP and Windows&nbsp;2003. The information provided here is therefore intended for troubleshooting and debugging purposes and not for production use.) [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[26],"class_list":["post-28763","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-other"],"acf":[],"blog_post_summary":"<p>Internally, a critical section is a bunch of counters and flags, and possibly an event. (Note that the internal structure of a critical section is subject to change at any time&mdash;in fact, it changed between Windows&nbsp;XP and Windows&nbsp;2003. The information provided here is therefore intended for troubleshooting and debugging purposes and not for production use.) [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/28763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=28763"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/28763\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=28763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=28763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=28763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}