2

so, I load Dll with some global static variables(loggers). and there is no freelibrary. on close application I call methods from dll but global static variables already destroyed. why???

Anthony Williams
  • 66,628
  • 14
  • 133
  • 155
  • When exactly do you call those methods? – sharptooth Mar 02 '11 at 07:28
  • Could you give an example of what you mean? What are these static variables, how do you initialize them, do you assign anything to them from the application, etc. – Collin Dauphinee Mar 02 '11 at 07:32
  • Avoid static objects in DLL unless they are DllMain-safe. For example objects containing FILE* or std::fstream are *NOT* DllMain-safe. See recent thread here http://stackoverflow.com/questions/5114683/loading-dll-not-initializing-static-c-classes/5115008#5115008 and this article http://go.microsoft.com/FWLink/?LinkId=84138 – atzz Mar 02 '11 at 09:20

1 Answers1

2

The static variables in the DLL will be destroyed when the DLL is unloaded. This will happen when the process exits, before the static variables from the main EXE are destroyed, if there is no explicit FreeLibrary call to make it happen earlier.

Anthony Williams
  • 66,628
  • 14
  • 133
  • 155
  • `static bool Loggable; class Flag{ public: bool &live_detector_; Flag(bool &live_detector):live_detector_(live_detector){ live_detector_ = true; } ~Flag(){ live_detector_ = false; } }; Flag f(Loggable);`??? – kain64b kain64b Mar 02 '11 at 08:09