What happens to global variables declared in a DLL?

后端 未结 6 732
眼角桃花
眼角桃花 2020-11-27 14:47

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?

6条回答
  •  独厮守ぢ
    2020-11-27 15:10

    In windows binary image files with extension *.exe, *.dll are in PE format Such files have Entry Point. You can view it with dumpbin tool like

    dumpbin /headers dllname.dll

    If you use C runtime from Microsoft, then your entry point will be something like *CRTStartup or *DllMainCRTStartup

    Such functions perform initialization of c and c++ runtime and delegate execution to (main, WinMain) or to DllMain respectively.

    If you use Microsofts VC compiler then you can watch at source code of this functions in yours VC directory:

    • crt0.c
    • dllcrt0.c

    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:

    • main or WinMain of startup thread of program returns control flow
    • you explictly call FreeLibrary and use-dll-counter is zero

提交回复
热议问题