{"id":44483,"date":"2015-03-12T07:00:00","date_gmt":"2015-03-12T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2015\/03\/12\/under-what-conditions-will-the-iunknownaddref-method-return-0\/"},"modified":"2019-03-13T12:13:43","modified_gmt":"2019-03-13T19:13:43","slug":"20150312-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20150312-00\/?p=44483","title":{"rendered":"Under what conditions will the IUnknown::AddRef method return 0?"},"content":{"rendered":"<p>A customer was debugging their application and discovered that for one of the objects they were using, the <code>IUnknown::Add&shy;Ref<\/code> method returns 0. How is that possible? That would imply that the object&#8217;s reference count was originally negative one? <\/p>\n<p>The return value from <code>IUnknown::Add&shy;Ref<\/code> is the object reference count by convention, but  <\/p>\n<blockquote CLASS=\"q\"><p>This value is intended to be used <a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms691379(v=vs.85).aspx\">only for test purposes<\/a>. <\/p><\/blockquote>\n<p>The return value is purely advisory and is not required to be accurate. <\/p>\n<p>For example, if the object is a proxy, it will most likely return the reference count of the local proxy rather than the raw reference count of the original object. Conversely, if you have an object with outstanding proxies, the <code>IUnknown::Add&shy;Ref<\/code> will count only one reference per proxy, even if the proxies themselves have reference counts greater than one. <\/p>\n<p>The object the customer was using came from <code>MSHTML.DLL<\/code>, and it so happens that the implementation of <code>IUnknown::Add&shy;Ref<\/code> used by that component always returns zero. It is technically within their rights to do so. <\/p>\n<p>I don&#8217;t know for sure, but I suspect this is done on purpose to avoid applications relying on the exact reference count. Applications are known to do dubious things, such as <a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/windows\/desktop\/ee416788(v=vs.100).aspx#What_do_the_return_values_from_the_Release_or_AddRef_methods_mean__\">call <code>IUnknown:&shy;Release<\/code> in a loop until it says the reference count is zero<\/a>. Making the objects return a value from <code>IUnknown::Add&shy;Ref<\/code> that betrays no information about the object&#8217;s true reference count may have been a defensive step to prevent applications from making any such dubious dependency. <\/p>\n<p>If you install the debugging version of <code>MSHTML.DLL<\/code>, then the <code>IUnknown::Add&shy;Ref<\/code> method will return the reference count. Which makes sense in its own way because the value is intended to be used only when debugging. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>When it&#8217;s lying.<\/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-44483","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>When it&#8217;s lying.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/44483","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=44483"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/44483\/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=44483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=44483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=44483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}