{"id":111335,"date":"2025-07-03T07:00:00","date_gmt":"2025-07-03T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=111335"},"modified":"2025-07-03T09:24:15","modified_gmt":"2025-07-03T16:24:15","slug":"20250703-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20250703-00\/?p=111335","title":{"rendered":"If the <CODE>Format&shy;Message<\/CODE> function fails, and I requested that it allocate a buffer, do I have to free the buffer?"},"content":{"rendered":"<p>A customer called the <code>Format\u00adMessage<\/code> function with the <code>FORMAT_<wbr \/>MESSAGE_<wbr \/>ALLOCATE_<wbr \/>BUFFER<\/code> flag, and they weren&#8217;t sure what to do if the function fails (by returning 0). Do they have to free the buffer by calling <code>Local\u00adFree<\/code>?<\/p>\n<p>No, you don&#8217;t have to free the buffer. In fact, on failure, there is no buffer. The function failed to perform the desired operation, so there is nothing to clean up.<\/p>\n<p>You can make things easier on yourself by pre-initializing the output pointer to <code>NULL<\/code>. That way, if the function fails, the pointer is still null. Then your logic can be &#8220;Go ahead and free the buffer,&#8221; because the <code>Local\u00adFree<\/code> function allows you to pass <code>NULL<\/code>, and it just ignores it. (This trick allows things like <code>wil::unique_hlocal_string<\/code> to work with <code>FormatMessage<\/code>.)<\/p>\n<p>Thinking about the original question: You can&#8217;t tell whether the reason for the function failure is that something went wrong during formatting or that something went wrong during allocation of the final output buffer. You could call <code>Get\u00adLast\u00adError()<\/code>, but if it returns <code>ERROR_<wbr \/>OUT_<wbr \/>OF_<wbr \/>MEMORY<\/code>, you still don&#8217;t know whether it ran out of memory <a title=\"Fancy use of exception handling in FormatMessage leads to repeated &quot;discovery&quot; of security flaw\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20120210-00\/?p=8333\"> during the formatting phase<\/a> or during the final buffer allocation phase. Therefore, even if you wanted to free the buffer, you don&#8217;t know whether you even got one in the first place.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There was no buffer returned, so there&#8217;s nothing to free anyway.<\/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-111335","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>There was no buffer returned, so there&#8217;s nothing to free anyway.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/111335","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=111335"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/111335\/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=111335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=111335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=111335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}