{"id":92711,"date":"2015-12-25T07:00:00","date_gmt":"2015-12-25T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=92711"},"modified":"2019-03-13T12:23:14","modified_gmt":"2019-03-13T19:23:14","slug":"20151225-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20151225-00\/?p=92711","title":{"rendered":"When is the correct time to call FreeLibraryWhenCallbackReturns?"},"content":{"rendered":"<p>When is the correct time to call <code>Free&shy;Library&shy;When&shy;Callback&shy;Returns<\/code>, or any of the other <code>...When&shy;Callback&shy;Returns<\/code> functions? In practice, many people call the function immediately before returning from the callback. Is this a requirement? <\/p>\n<p>No. You can call the <code>...When&shy;Callback&shy;Returns<\/code> function at any time during the execution of your callback, but you can call each one at most once per callback instance. In other words, you cannot ask for two DLLs to be freed when the callback returns, but it&#8217;s okay to ask for one DLL to be freed and one critical section to be exited. If you do ask for multiple things to happen when the callback returns, the order in which they occur is unspecified. <\/p>\n<p>By convention, the <code>...When&shy;Callback&shy;Returns<\/code> function is called immediately before the callback returns, because it matches the point at which you would have called the non-<code>...When&shy;Callback&shy;Returns<\/code> version. In other words, &#8220;I would have called <code>Free&shy;Library<\/code> or <code>Free&shy;Library&shy;And&shy;Exit&shy;Thread<\/code> here, but I can&#8217;t, so I&#8217;ll call <code>Free&shy;Library&shy;When&shy;Callback&shy;Returns<\/code> instead, and then immediately return.&#8221; <\/p>\n<p>Of course, if you are worried that you might forget to call <code>Free&shy;Library&shy;When&shy;Callback&shy;Returns<\/code> in all your exit paths (a legitimate concern, in my opinion), you may choose to call it at the start of the work, so that you won&#8217;t forget. It doesn&#8217;t matter when you call it, as long as you do it inside the task at some point before you return. <\/p>\n<p>Many happy returns. (Tomorrow is Boxing Day, you see.) <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Any time during the execution of the callback is fine.<\/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-92711","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Any time during the execution of the callback is fine.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/92711","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=92711"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/92711\/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=92711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=92711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=92711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}