{"id":15563,"date":"2009-12-23T07:00:00","date_gmt":"2009-12-23T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2009\/12\/23\/why-is-it-possible-to-destroy-a-critical-section-while-it-is-in-use\/"},"modified":"2009-12-23T07:00:00","modified_gmt":"2009-12-23T07:00:00","slug":"why-is-it-possible-to-destroy-a-critical-section-while-it-is-in-use","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20091223-00\/?p=15563","title":{"rendered":"Why is it possible to destroy a critical section while it is in use?"},"content":{"rendered":"<p>\nSome time back,\nStu wondered\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/12\/11\/1259599.aspx#1263540\">\nwhy it is possible to destroy a critical section while it is in use<\/a>.\n<\/p>\n<p>\nWell, there&#8217;s nothing stopping you from creating a file that contains\nthese lines:\n<\/p>\n<pre>\n#include &lt;windows.h&gt;\nint __cdecl main(int, char**)\n{\n  CRITICAL_SECTION cs;\n  InitializeCriticalSection(&amp;cs);\n  EnterCriticalSection(&amp;cs);\n  DeleteCriticalSection(&amp;cs);\n  return 0;\n}\n<\/pre>\n<p>\nand then telling your compiler to turn it into a program.\nIt&#8217;s not like a bolt of lightning is going to come out of the sky\nand zap you before you hit the Enter key.\n<\/p>\n<p>\nSo obviously, it&#8217;s possible.\n<\/p>\n<p>\nOn the other hand, it&#8217;s a bug,\njust like closing a handle to a file that another thread is\nreading from,\nor like closing an event handle that another thread is waiting on.\n<\/p>\n<p>\nCritical sections are one of those low-level <i>I sure hope you know what\nyou&#8217;re doing because I&#8217;m not going to help you if you mess up<\/i>\npieces of functionality.\nIf you use them incorrectly, then you will suffer the consequences,\nthe same as if you tried to free memory twice\nor write to memory after freeing it\nor cast a <code>Gdiplus::Color*<\/code> to a <code>CComBSTR*<\/code>.\n<\/p>\n<p>\nAre there any legitimate cases where you would delete a critical\nsection while it is owned?\nI sure can&#8217;t think of any.\n<\/p>\n<p>\nIf there were a legitimate case for deleting a critical section\nwhile it is owned, what could it be?\nWell, it can&#8217;t be owned by the thread doing the deleting,\nbecause that would imply that you took it in order to prevent\nsomebody else from entering it (while you deleted it),\nbut that just creates another race condition:\nIf you tinker the timing, then you can create this\nscenario:\nThat other thread gets\npre-empted just as it was about to execute the first\ninstruction of the <code>EnterCriticalSection<\/code> function.\nMeanwhile, the destroying thread enters the critical section,\ndoes whatever other stuff it wants to do,\nand then deletes the critical section.\nThat other thread finally gets a chance to run and is now\nattempting to enter a deleted critical section, which is clearly\nnot legal.\n<\/p>\n<p>\nOkay, so if there were a legitimate case, it would have to be\ndeleting a critical section owned by some other thread.\nMaybe that other thread enters the critical section, and then\nsignals the main thread to delete the critical section.\nWhy would it do that? Who knows.\nMaybe it wants to make sure only one thread signals\nthe main thread.\nBut you still have the same problem as with the previous case:\nYou entered the critical section because you wanted to prevent\na third thread from entering the protected region,\nbut that third thread might have been pre-empted just as it\ntransferred control to the first instruction of\n<code>EnterCriticalSection<\/code>,\nand when that third thread finally gets some CPU time,\nit proceeds to enter a deleted critical section.\n<\/p>\n<p>\nSo I can&#8217;t think of a legitimate reason for deleting a critical\nsection while it&#8217;s in use.\nMaybe there&#8217;s a flaw in my logic.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some time back, Stu wondered why it is possible to destroy a critical section while it is in use. Well, there&#8217;s nothing stopping you from creating a file that contains these lines: #include &lt;windows.h&gt; int __cdecl main(int, char**) { CRITICAL_SECTION cs; InitializeCriticalSection(&amp;cs); EnterCriticalSection(&amp;cs); DeleteCriticalSection(&amp;cs); return 0; } and then telling your compiler to turn it [&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":[25],"class_list":["post-15563","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Some time back, Stu wondered why it is possible to destroy a critical section while it is in use. Well, there&#8217;s nothing stopping you from creating a file that contains these lines: #include &lt;windows.h&gt; int __cdecl main(int, char**) { CRITICAL_SECTION cs; InitializeCriticalSection(&amp;cs); EnterCriticalSection(&amp;cs); DeleteCriticalSection(&amp;cs); return 0; } and then telling your compiler to turn it [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/15563","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=15563"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/15563\/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=15563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=15563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=15563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}