{"id":8663,"date":"2012-01-06T07:00:00","date_gmt":"2012-01-06T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2012\/01\/06\/why-did-heapfree-fail-with-error_possible_deadlock\/"},"modified":"2012-01-06T07:00:00","modified_gmt":"2012-01-06T07:00:00","slug":"why-did-heapfree-fail-with-error_possible_deadlock","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20120106-00\/?p=8663","title":{"rendered":"Why did HeapFree fail with ERROR_POSSIBLE_DEADLOCK?"},"content":{"rendered":"<p>A customer reported that they were receiving some assertion failures because the <code>Heap&shy;Free<\/code> function was failing with what they believed to be a valid heap block, and the <code>Get&shy;Last&shy;Error<\/code> function reported that the reason for failure was <code>ERROR_POSSIBLE_DEADLOCK<\/code>. What&#8217;s going on?\n One of my colleagues asked the psychic question, &#8220;Is the process exiting?&#8221;\n &#8220;Why yes, in fact it is. How did you know?&#8221;\n Recall <a href=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2007\/05\/03\/2383346.aspx\"> how processes exit<\/a>. One of the first things that happens is that all the other threads in the process are forcible terminated, which has as a consequence that any synchronization resources owned by those threads are now orphaned. And in this case, the synchronization resource in question was the heap.\n When the function calls <code>Heap&shy;Free<\/code>, the heap code tries to take the <a href=\"http:\/\/msdn.microsoft.com\/library\/aa366702.aspx\"> heap lock<\/a> but finds that it can&#8217;t because the heap lock was owned by another thread. And that other thread no longer exists. (Perhaps it was terminated while it was in the middle of its own <code>Heap&shy;Free<\/code> operation.) The heap code detects this and instead of deadlocking on its own custom synchronization object, it fails with the error <code>ERROR_POSSIBLE_DEADLOCK<\/code>.<\/p>\n<p> By the same logic, you can demonstrate that you cannot reliably allocate memory at process shutdown either. So now you can&#8217;t allocate memory; you can&#8217;t free memory. As we saw last time, when you are told that the process is exiting, <a href=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2012\/01\/05\/10253268.aspx\"> you should not do any cleanup at all<\/a>. The memory will get freed when the process address space is torn down. No need to free it manually; that&#8217;s just a waste of time. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer reported that they were receiving some assertion failures because the Heap&shy;Free function was failing with what they believed to be a valid heap block, and the Get&shy;Last&shy;Error function reported that the reason for failure was ERROR_POSSIBLE_DEADLOCK. What&#8217;s going on? One of my colleagues asked the psychic question, &#8220;Is the process exiting?&#8221; &#8220;Why yes, [&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-8663","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer reported that they were receiving some assertion failures because the Heap&shy;Free function was failing with what they believed to be a valid heap block, and the Get&shy;Last&shy;Error function reported that the reason for failure was ERROR_POSSIBLE_DEADLOCK. What&#8217;s going on? One of my colleagues asked the psychic question, &#8220;Is the process exiting?&#8221; &#8220;Why yes, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/8663","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=8663"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/8663\/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=8663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=8663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=8663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}