{"id":39233,"date":"2004-05-21T07:02:00","date_gmt":"2004-05-21T07:02:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2004\/05\/21\/do-you-know-when-your-destructors-run-part-2\/"},"modified":"2004-05-21T07:02:00","modified_gmt":"2004-05-21T07:02:00","slug":"do-you-know-when-your-destructors-run-part-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040521-00\/?p=39233","title":{"rendered":"Do you know when your destructors run? Part 2."},"content":{"rendered":"<p>\nContinuing from\n<a href=\"http:\/\/weblogs.asp.net\/oldnewthing\/archive\/2004\/05\/20\/135841.aspx\">yesterday<\/a>,\nhere&#8217;s another case where you have to watch your destructors.\nYesterday&#8217;s theme was destructors that run at the wrong time.\nToday, we&#8217;re going to see destructors that don&#8217;t run at all!\n<\/p>\n<p>\nAssume there&#8217;s an ObjectLock class which\ntakes a lock in its constructor\nand releases it in its destructor.<\/p>\n<pre>\nDWORD ThreadProc(LPVOID p)\n{\n  ... do stuff ...\n  ObjectLock lock(p);\n  ... do stuff ...\n  return 0;\n}\n<\/pre>\n<p>\nPretty standard stuff.\nThe first batch of stuff is done without the lock,\nand the second batch is done inside the lock.\nWhen the function returns, the lock is automatically released.\n<\/p>\n<p>\nBut suppose somebody adds a little code to this function like this:<\/p>\n<pre>\nDWORD ThreadProc(LPVOID p)\n{\n  ... do stuff ...\n  ObjectLock lock(p);\n  ...\n  if (p-&gt;cancelled) ExitThread(1);\n  ...\n  return 0;\n}\n<\/pre>\n<p>\nThe code change was just to add an early exit if the object was\ncancelled.\n<\/p>\n<p>\nBut when does that ObjectLock destructor run?\n<\/p>\n<p>\nIt runs at the <code>return<\/code> statement, since that&#8217;s when the\n<code>lock<\/code> goes out of scope.  In particular, it is\n<strong>not<\/strong> run before you call <code>ExitThread<\/code>.\n<\/p>\n<p>\nResult: You left an object locked permanently.\n<\/p>\n<p>\nYou can imagine how variations on this code could lead to\nresource leaks or other problems.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Continuing from yesterday, here&#8217;s another case where you have to watch your destructors. Yesterday&#8217;s theme was destructors that run at the wrong time. Today, we&#8217;re going to see destructors that don&#8217;t run at all! Assume there&#8217;s an ObjectLock class which takes a lock in its constructor and releases it in its destructor. DWORD ThreadProc(LPVOID p) [&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-39233","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Continuing from yesterday, here&#8217;s another case where you have to watch your destructors. Yesterday&#8217;s theme was destructors that run at the wrong time. Today, we&#8217;re going to see destructors that don&#8217;t run at all! Assume there&#8217;s an ObjectLock class which takes a lock in its constructor and releases it in its destructor. DWORD ThreadProc(LPVOID p) [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/39233","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=39233"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/39233\/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=39233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=39233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=39233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}