<p>Let's say I write a DLL in C++, and declare a global object of a class with a non-trivial destructor.  Will the destructor be called when the DLL is unloaded?</p>

[{'answer_id': 75788, 'author': 'Philip Rieck', 'author_id': 12643, 'author_profile': 'https://Stackoverflow.com/users/12643', 'pm_score': 2, 'selected': False, 'text': "<p>It should be called when either the application ends or the DLL is unloaded, whichever comes first.  Note that this is somewhat dependent on the actual runtime you're compiling against.  </p>\n\n<p>Also, beware non-trivial destructors as there are both timing and ordering issues. Your DLL may be unloaded <strong>after</strong> a DLL your destructor relies on, which would obviously cause issues.</p>\n"}, {'answer_id': 75855, 'author': 'INS', 'author_id': 13136, 'author_profile': 'https://Stackoverflow.com/users/13136', 'pm_score': 1, 'selected': False, 'text': '<p>When DllMain with fdwReason = DLL_PROCESS_DETACH parameter is called it means the DLL is unloaded by the application. This is the time before the destructor of global/static objects gets called.</p>\n'}, {'answer_id': 76273, 'author': 'Mark Ransom', 'author_id': 5987, 'author_profile': 'https://Stackoverflow.com/users/5987', 'pm_score': 3, 'selected': False, 'text': '<p>This page from Microsoft goes into the details of DLL initialization and destruction of globals:<br>\n<a href="http://msdn.microsoft.com/en-us/library/988ye33t.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/988ye33t.aspx</a></p>\n'}, {'answer_id': 76677, 'author': 'MSN', 'author_id': 6210, 'author_profile': 'https://Stackoverflow.com/users/6210', 'pm_score': 3, 'selected': False, 'text': '<p>If you want to see the actual code that gets executed when linking a .dll, take a look at <code>%ProgramFiles%\\Visual Studio 8\\vc\\crt\\src\\dllcrt0.c</code>.</p>\n\n<p>From inspection, destructors will be called via <code>_cexit()</code> when the internal reference count maintained by the dll CRT hits zero.</p>\n'}, {'answer_id': 78235, 'author': 'paercebal', 'author_id': 14089, 'author_profile': 'https://Stackoverflow.com/users/14089', 'pm_score': 6, 'selected': True, 'text': '<p>In a Windows C++ DLL, all global objects (including static members of classes) will be constructed just before the calling of the DllMain with DLL_PROCESS_ATTACH, and they will be destroyed just after the call of the DllMain with DLL_PROCESS_DETACH.</p>\n<p>Now, you must consider three problems:</p>\n<p>0 - Of course, global non-const objects are evil (but you already know that, so I\'ll avoid mentionning multithreading, locks, god-objects, etc.)</p>\n<p>1 - The order of construction of objects or different compilation units (i.e. CPP files) is not guaranteed, so you can\'t hope the object A will be constructed before B if the two objects are instanciated in two different CPPs. This is important if B depends on A. The solution is to move all global objects in the same CPP file, as inside the same compilation unit, the order of instanciation of the objects will be the order of construction (and the inverse of the order of destruction)</p>\n<p>2 - There are things that are forbidden to do in the DllMain. Those things are probably forbidden, too, in the constructors. So avoid locking something. See Raymond Chen\'s excellent blog on the subject:</p>\n<ul>\n<li><a href="https://devblogs.microsoft.com/oldnewthing/20040127-00/?p=40873" rel="nofollow noreferrer">Some reasons not to do anything scary in your DllMain</a></li>\n<li><a href="https://devblogs.microsoft.com/oldnewthing/20040128-00/?p=40853" rel="nofollow noreferrer">Another reason not to do anything scary in your DllMain: Inadvertent deadlock</a></li>\n<li><a href="https://devblogs.microsoft.com/oldnewthing/20140821-00/?p=183" rel="nofollow noreferrer">Some reasons not to do anything scary in your DllMain, part 3</a></li>\n</ul>\n<p>In this case, lazy initialization could be interesting: The classes remain in an &quot;un-initialized&quot; state (internal pointers are NULL, booleans are false, whatever) until you call one of their methods, at which point they\'ll initialize themselves. If you use those objects inside the main (or one of the main\'s descendant functions), you\'ll be ok because they will be called after execution of DllMain.</p>\n<p>3 - Of course, if some global objects in DLL A depend on global objects in DLL B, you should be very very careful about DLL loading order, and thus dependancies. In this case, DLLs with direct or indirect circular dependancies will cause you an insane amount of headaches. The best solution is to break the circular dependancies.</p>\n<p>P.S.: Note that in C++, constructor can throw, and you don\'t want an exception in the middle of a DLL loading, so be sure your global objects won\'t be using exception without a very, very good reason. As correctly written destructors are not authorized to throw, the DLL unloading should be ok in this case.</p>\n'}, {'answer_id': 31164412, 'author': 'Konstantin Burlachenko', 'author_id': 1154447, 'author_profile': 'https://Stackoverflow.com/users/1154447', 'pm_score': 2, 'selected': False, 'text': '<p>In windows binary image files with extension *.exe, *.dll are in <a href="https://msdn.microsoft.com/en-us/windows/hardware/gg463119.aspx" rel="nofollow">PE format</a>\nSuch files have Entry Point. You can view it with dumpbin tool like </p>\n\n<blockquote>\n  <p>dumpbin /headers dllname.dll</p>\n</blockquote>\n\n<p>If you use C runtime from Microsoft, then your entry point will be something like\n *CRTStartup or *DllMainCRTStartup</p>\n\n<p>Such functions perform initialization of c and c++ runtime and delegate execution to (main, WinMain) or to DllMain respectively.</p>\n\n<p>If you use Microsofts VC compiler then you can watch at source code of this functions in yours VC directory:</p>\n\n<ul>\n<li>crt0.c</li>\n<li>dllcrt0.c</li>\n</ul>\n\n<p>DllMainCRTStartup process all things need to init/deinit your global variables from .data sections in normal scenario, when it retrive notification DLL_PROCESS_DETACH during dll unload. For example:</p>\n\n<ul>\n<li>main or WinMain of startup thread of program returns control flow</li>\n<li>you explictly call FreeLibrary and use-dll-counter is zero</li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/75701', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/13313/']