{"id":104048,"date":"2020-08-06T07:00:00","date_gmt":"2020-08-06T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=104048"},"modified":"2020-08-06T07:31:30","modified_gmt":"2020-08-06T14:31:30","slug":"20200806-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200806-00\/?p=104048","title":{"rendered":"What was so horrible about the FORMAT_MESSAGE_ALLOCATE_BUFFER flag that the Windows Store disallowed it for so long?"},"content":{"rendered":"<p><a title=\"What does it mean when it says that FORMAT_MESSAGE_ALLOCATE_BUFFER can be used in Store apps starting in Windows 10?\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200805-00\/?p=104046\"> Last time<\/a>, we learned about the tumultuous history of the <code>FORMAT_<\/code><code>MESSAGE_<\/code><code>ALLOCATE_<\/code><code>BUFFER<\/code> flag in Windows Store UWP apps.<\/p>\n<p>But why was this flag disallowed for so long?<\/p>\n<p>It&#8217;s nothing particularly profound. Rather, it was just bad luck.<\/p>\n<p>The buffer allocated by the <code>FORMAT_<\/code><code>MESSAGE_<\/code><code>ALLOCATE_<\/code><code>BUFFER<\/code> flag needs to be freed by calling <code>Local\u00adFree<\/code>, but <code>Local\u00adFree<\/code> was not one of the functions that can be called from a Windows Store app.<\/p>\n<p>Why not?<\/p>\n<p>Because <code>Local\u00adAlloc<\/code> and <code>Local\u00adFree<\/code> are legacy functions that hang around for backward compatibility with 16-bit Windows. New programs shouldn&#8217;t be using them. It&#8217;s not like your new program needs to be backward compatible with 16-bit Windows 3.1.<\/p>\n<p>But this left the <code>FORMAT_<\/code><code>MESSAGE_<\/code><code>ALLOCATE_<\/code><code>BUFFER<\/code> flag in a bit of a pickle, because despite being something new for Win32, the flag continues to use that old and busted legacy function for memory allocation.<\/p>\n<p>There was some discussion within the team about how to address the problem. One school of thought was to document enough of the internals of the <code>Local\u00adFree<\/code> function so that you could call <code>Heap\u00adFree<\/code> to free it. You can see remnants of this approach in the comments of the <code>winbase.h<\/code> header file:<\/p>\n<pre>\/\/\r\n\/\/ FORMAT_MESSAGE_ALLOCATE_BUFFER requires use of HeapFree\r\n\/\/\r\n<\/pre>\n<p>For a while, there was also this paragraph of the <code>FORMAT_<\/code><code>MESSAGE_<\/code><code>ALLOCATE_<\/code><code>BUFFER<\/code> documentation that says a bunch of stuff in a rather confusing way.<\/p>\n<blockquote class=\"q\"><p><b>Windows 10<\/b>: <b>LocalAlloc()<\/b> has different options: <b>LMEM_FIXED<\/b>, and <b>LMEM_MOVABLE<\/b>. <b>FormatMessage()<\/b> uses <b>LMEM_FIXED<\/b>, so <b>HeapFree<\/b> can be used. If <b>LMEM_MOVABLE<\/b> is used, <b>HeapFree<\/b> cannot be used.<\/p><\/blockquote>\n<p>(Fortunately, the confusing paragraph isn&#8217;t there any more.)<\/p>\n<p>The decision that won the day was to accept that legacy code will never die. The team just held their nose and added <code>LocalAlloc<\/code> and <code>LocalFree<\/code> to the list of functions that are permitted to be called by Windows Store app.<\/p>\n<p>But please, promise to use it only for situations that absolutely require it for compatibility purposes, like freeing the message string allocated by <code>Format\u00adMessage<\/code>. Don&#8217;t use it as your go-to memory allocation function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Letting in the riffraff.<\/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-104048","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>Letting in the riffraff.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104048","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=104048"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104048\/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=104048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=104048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=104048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}