User:Kr/A DLL's Life
Status: draft
We are frequently facing problems with globally constructed and destructed values in C / C++, mostly leading to crashes while terminating. E.g. a shared library has a a global reference holding an object implemented in another shared library.
Suggested Solutions:
- Don't use globals.
- Find a pattern for globals to be usable.
There are discussions doubting the usefulness of globals, its proponents advocating the former solution suggestion, personally I think globals need to supported and must usable as expected, for the simpleness of programming. Therefore I propose the falling usage pattern for shared libraries, seemingly leading to the expected behaviour.
Dynamic Loading
- Dependency cycles between shared libraries need to be handled explicitly.
- A dynamically loaded library may not be unloaded as long as there is some reference to it's content (e.g. an objects vtable).
- All dynamically opened shared libraries need to be dynamically closed before "exit", in the order of dependence.
Linking
As using a link editors link facility does not give programmatical access to load and unload order, it must be ensured, that a lower dependency library does not depend on the availability of a higher dependency library:
- If library A depends on library B, than B must not have a dependency to A during process termination.
C++
As because of C++'s relationship to "atexit", an executable may not rely on dynamic unloading of a C++ library while using any of its globals, all of the executables globals need to be cleared (thus leading C++ libraries to be unloaded) before exiting.