{"id":102719,"date":"2019-07-18T07:00:00","date_gmt":"2019-07-18T14:00:00","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/oldnewthing\/?p=102719"},"modified":"2019-07-17T21:09:21","modified_gmt":"2019-07-18T04:09:21","slug":"20190718-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190718-00\/?p=102719","title":{"rendered":"After I made my DLL delay-load another DLL, my DLL has started crashing in its process detach code"},"content":{"rendered":"<p>A customer had a DLL, let&#8217;s call it <code>CONTOSO.DLL<\/code>, and that DLL linked to another DLL, let&#8217;s call it <code>WIDGET.DLL<\/code>. To improve DLL load time, they made <code>WIDGET.DLL<\/code> a delay-loaded DLL via <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/build\/reference\/delayload-delay-load-import\"> the <code>\/DELAYLOAD<\/code> linker option<\/a>. This worked out great, except that sometimes their DLL crashed when shutting down.<\/p>\n<p>When the <code>WIDGET.DLL<\/code> was a static dependency, the loader made a note to ensure that <code>WIDGET.DLL<\/code> was loaded and ready before calling <code>CONTOSO.DLL<\/code>&#8216;s initialization function, and made sure that <code>WIDGET.DLL<\/code> remained valid until <code>CONTOSO.DLL<\/code> completed its uninitialization.<\/p>\n<p>Switching the <code>WIDGET.DLL<\/code> to a <code>\/DELAYLOAD<\/code> DLL removes this static dependency, and the loader isn&#8217;t around to help any more.<\/p>\n<p>When the process shuts down, the loader uninitializes the DLLs in an order that tries\u00b9 to preserve the static dependencies, so that a DLL waits until all its dependents are uninitialized before itself uninitializing. However, the loader does not have insight into dynamically-created dependencies, and the DLLs may <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050523-05\/?p=35573\"> unload out of order<\/a>.<\/p>\n<p>What happened is that <code>CONTOSO.DLL<\/code> initialized without <code>WIDGET.DLL<\/code>, and then later somebody needed a widget, so it loaded <code>WIDGET.DLL<\/code> and did some widget stuff, and then cached the widget so it wouldn&#8217;t have to go through all that nonsense again.<\/p>\n<p>In the <code>CONTOSO.DLL<\/code> module&#8217;s <code>DLL_<code><\/code>PROCESS_<code><\/code>DETACH<\/code> code, it checks\u00b2 if there is a cached widget, and if so, destroys it.<\/p>\n<p>WIDGET.DLL was a dynamic dependency, the module loader doesn&#8217;t take it into account when calculating the order in which modules should be uninitlalized. The loader sees no static dependency between <code>CONTOSO.DLL<\/code> and <code>WIDGET.DLL<\/code>, so the order in which they uninitialize is arbitrary.<\/p>\n<p>And if the arbitrary decision ends up selecting <code>WIDGET.DLL<\/code> to uninitialize first, then you have a crash when <code>CONTOSO.DLL<\/code> tries to call into an already-uninitialized DLL.<\/p>\n<p>Note that this problem occurs only at process shutdown. If <code>CONTOSO.DLL<\/code> unloads via a runtime call to <code>Free\u00adLibrary<\/code>, it will still be able to call into <code>WIDGET.DLL<\/code> because it hasn&#8217;t yet called <code>Free\u00adLibrary<\/code> on <code>WIDGET.DLL<\/code>. But during process shutdown, the module loader needs to free all the things, and the outstanding <code>Load\u00adLibrary<\/code> won&#8217;t prevent that from happening.<\/p>\n<p>The solution is to bypass widget cleanup if the <code>DLL_<code><\/code>PROCESS_<code><\/code>DETACH<\/code> handler realizes that the process is terminating. Just leak the widget. <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20120105-00\/?p=8683\"> The building is being demolished. You don&#8217;t need to sweep the floors<\/a>.<\/p>\n<p>The DLL was able to start without the widget DLL. It should be able to finish without the widget DLL.<\/p>\n<p>\u00b9 I say &#8220;tries&#8221; because circular dependencies make such an effort impossible to achieve, but the loader does the best it can.<\/p>\n<p>\u00b2 It&#8217;s important to check for evidence of widgets before trying to clean up widget-related things. Otherwise, you may end up <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20100115-00\/?p=15253\"> loading a DLL in your <code>DLL_<code><\/code>PROCESS_<code><\/code>DETACH<\/code> handler<\/a>, and that&#8217;s not good.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You started without it. You should be able to finish without it.<\/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-102719","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You started without it. You should be able to finish without it.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102719","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=102719"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102719\/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=102719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=102719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=102719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}