{"id":29823,"date":"2006-09-07T10:00:04","date_gmt":"2006-09-07T10:00:04","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2006\/09\/07\/you-have-to-free-memory-with-the-same-allocator-that-allocated-it-logical-consequences\/"},"modified":"2006-09-07T10:00:04","modified_gmt":"2006-09-07T10:00:04","slug":"you-have-to-free-memory-with-the-same-allocator-that-allocated-it-logical-consequences","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20060907-04\/?p=29823","title":{"rendered":"You have to free memory with the same allocator that allocated it: Logical consequences"},"content":{"rendered":"<p>Everybody should know by now that you have to free memory using the same allocator that you used to allocate the memory. If you allocate with <code>new[]<\/code> then you have to free with <code>delete[]<\/code>; if you allocate with <code>LocalAlloc<\/code> then you have to free with <code>LocalFree<\/code>.\n Once you&#8217;ve internalized this rule, you can use it to draw other logical conclusions. Consider:<\/p>\n<blockquote class=\"q\"><p> When I call the <code>PropertySheet<\/code> function, who is responsible for freeing the memory that was allocated for the <code>phpage<\/code> field of the <code>PROPSHEETHEADER<\/code> structure? <\/p><\/blockquote>\n<p> Well, there are two candidates for this responsibility, either the <code>PropertySheet<\/code> function or the caller of the <code>PropertySheet<\/code> function. If the <code>PropertySheet<\/code> function was responsible for freeing the memory, it would have to make sure to use the same allocator that was used to allocate the <code>phpage<\/code>. But there is no requirement that that memory use any particular allocator. (In fact, a significant portion of the time, the memory is allocated from the stack, in which case there is no explicit deallocation step.) The <code>PropertySheet<\/code> function would now be required to be psychic and somehow &#8220;know&#8221; how the memory should be freed (or whether it should be freed at all). Since psychic powers have yet to be perfected in software, this pretty much closes off this line of reasoning.<\/p>\n<p> The only remaining candidate is the caller of the <code>PropertySheet<\/code> function. Since that&#8217;s the code that allocated the memory, it&#8217;s the one who knows how to free it. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Everybody should know by now that you have to free memory using the same allocator that you used to allocate the memory. If you allocate with new[] then you have to free with delete[]; if you allocate with LocalAlloc then you have to free with LocalFree. Once you&#8217;ve internalized this rule, you can use it [&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-29823","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Everybody should know by now that you have to free memory using the same allocator that you used to allocate the memory. If you allocate with new[] then you have to free with delete[]; if you allocate with LocalAlloc then you have to free with LocalFree. Once you&#8217;ve internalized this rule, you can use it [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/29823","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=29823"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/29823\/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=29823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=29823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=29823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}