{"id":20353,"date":"2008-11-03T10:00:00","date_gmt":"2008-11-03T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2008\/11\/03\/if-theres-already-a-bug-its-not-surprising-that-theres-a-possibility-for-error\/"},"modified":"2008-11-03T10:00:00","modified_gmt":"2008-11-03T10:00:00","slug":"if-theres-already-a-bug-its-not-surprising-that-theres-a-possibility-for-error","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20081103-00\/?p=20353","title":{"rendered":"If there&#8217;s already a bug, it&#8217;s not surprising that there&#8217;s a possibility for error"},"content":{"rendered":"<p>It&#8217;s great to think about all the things that can go wrong but you also have to think about the situations that could lead to those bad things. In particular, you have to recognize when you are trying to avoid a bug that is ultimately outside your component and which you can&#8217;t fix anyway.<\/p>\n<p> For example, consider <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/09\/28\/474855.aspx#474998\"> this multithreaded race condition<\/a>: <\/p>\n<blockquote class=\"q\"><p> Why is <code>InterlockedDecrement<\/code> used in the implementation of <code>IUnknown::Release<\/code>? The only reason I can think of is for multithread safety. But that <code>Release<\/code> function doesn&#8217;t look multithread safe&mdash;what if another thread was about to increment <code>m_cRef<\/code>? Does the <code>AddRef<\/code> refcount incrementer have a special interlocked check for zero to catch this case?  <\/p><\/blockquote>\n<p> What if another thread was about to increment <code>m_cRef<\/code>? In other words, what if another thread was about to call <code>IUnknown::AddRef<\/code>? In other words, you have two threads and an object with a refcount of one. One thread calls <code>Release<\/code> and the other thread calls <code>AddRef<\/code>. The concern is that the thread calling <code>AddRef<\/code> may execute after the thread that calls <code>Release<\/code>, thereby &#8220;rescuing&#8221; the reference count from zero back to one. <\/p>\n<p> But this scenario you&#8217;re worried about <i>is already a bug<\/i>. Suppose the second thread runs just a smidgen slower than the scenario you described, calling <code>AddRef<\/code> after the <code>Release<\/code> returns instead of while it is executing. Well, now, that&#8217;s obviously a bug in the caller, isn&#8217;t it? It&#8217;s using a pointer after destroying it. <\/p>\n<p> This happens a lot: You&#8217;re worrying about not introducing a bug into a hypothetical situation that is already a bug. The answer to that is &#8220;Fix the original bug.&#8221; <\/p>\n<p> In this specific situation of reference counting, a useful rule of thumb is &#8220;If you&#8217;re worrying about the possibility of a reference count incrementing from zero to one, then you already have a bug somewhere else.&#8221; <\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s great to think about all the things that can go wrong but you also have to think about the situations that could lead to those bad things. In particular, you have to recognize when you are trying to avoid a bug that is ultimately outside your component and which you can&#8217;t fix anyway. For [&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-20353","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>It&#8217;s great to think about all the things that can go wrong but you also have to think about the situations that could lead to those bad things. In particular, you have to recognize when you are trying to avoid a bug that is ultimately outside your component and which you can&#8217;t fix anyway. For [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/20353","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=20353"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/20353\/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=20353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=20353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=20353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}