{"id":20763,"date":"2008-09-25T10:00:00","date_gmt":"2008-09-25T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2008\/09\/25\/even-if-a-function-doesnt-do-anything-you-still-have-to-call-it-if-the-documentation-says-so-because-it-might-do-something-tomorrow\/"},"modified":"2008-09-25T10:00:00","modified_gmt":"2008-09-25T10:00:00","slug":"even-if-a-function-doesnt-do-anything-you-still-have-to-call-it-if-the-documentation-says-so-because-it-might-do-something-tomorrow","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20080925-00\/?p=20763","title":{"rendered":"Even if a function doesn&#8217;t do anything, you still have to call it if the documentation says so, because it might do something tomorrow"},"content":{"rendered":"<p>If the documentation says that you have to call a function, then you have to call it. It may be that the function doesn&#8217;t do anything, but that doesn&#8217;t prevent it from doing something in the future.<\/p>\n<p> Today&#8217;s example is the function <code>GetEnvironmentStrings<\/code>, which returns you all the environment variables of the current process in a single block, which you can then study at your leisure. When you&#8217;re finished, you&#8217;re supposed to call <code>FreeEnvironmentStrings<\/code>. That&#8217;s what the documentation says, and if you did that, then you&#8217;re in good shape. <\/p>\n<p> However, some people noticed that on Windows&nbsp;NT&nbsp;4, the Unicode version of the <code>FreeEnvironmentStrings<\/code> function didn&#8217;t do anything. In other words, the Unicode environment block didn&#8217;t need to be freed. When you called <code>GetEnvironmentStrings<\/code>, the kernel just returned you a raw pointer to the <i>real live<\/i> environment strings (which, since this is Windows&nbsp;NT, are kept in Unicode internally). Since nothing was allocated, there was nothing to free. <\/p>\n<p> The problem with this technique was that if somebody called <code>SetEnvironmentVariable<\/code> in the meantime, the environment block changed out from under the caller of <code>GetEnvironmentStrings<\/code>. <\/p>\n<p> Oops. <\/p>\n<p> To fix this, the <code>GetEnvironmentStrings<\/code> function was changed to return a copy of the environment block <i>even if you call the Unicode version<\/i>. The corresponding Unicode <code>FreeEnvironmentStrings<\/code> function frees that environment copy. <\/p>\n<p> Programs that followed the specification and called <code>FreeEnvironmentStrings<\/code> (even though it didn&#8217;t do anything up until now) were in good shape. Their call to <code>FreeEnvironmentStrings<\/code> now frees the memory, and all is right with the world. <\/p>\n<p> Programs that coded to the implementation rather than the specification are now in a world of hurt. If they simply skipped the &#8220;useless&#8221; call to <code>FreeEnvironmentStrings<\/code>, they will now find themselves leaking memory. On the other hand, if they gave lip service to <code>FreeEnvironmentStrings<\/code> by calling it, but using the memory anyway, they will find themselves accessing invalid heap memory, and all sorts of havoc can ensue. <\/p>\n<p> There&#8217;s sometimes a reason for the rules that seem stupid at first glance. (&#8220;Call this function that doesn&#8217;t do anything.&#8221;) Changes to the implementation may make them less stupid in the future. <\/p>\n<p> (Credit goes to my colleague Neill Clift for providing the information that led to today&#8217;s article.) <\/p>\n","protected":false},"excerpt":{"rendered":"<p>If the documentation says that you have to call a function, then you have to call it. It may be that the function doesn&#8217;t do anything, but that doesn&#8217;t prevent it from doing something in the future. Today&#8217;s example is the function GetEnvironmentStrings, which returns you all the environment variables of the current process in [&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":[2],"class_list":["post-20763","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>If the documentation says that you have to call a function, then you have to call it. It may be that the function doesn&#8217;t do anything, but that doesn&#8217;t prevent it from doing something in the future. Today&#8217;s example is the function GetEnvironmentStrings, which returns you all the environment variables of the current process in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/20763","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=20763"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/20763\/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=20763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=20763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=20763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}