I have a problem similar to this question: c++ linux double destruction of static variable. linking symbols overlap only my issue is with two vendor supplied libraries, so I do not have access to the code. The vendor has two dynamic libraries and linking against either one works fine, but linking against both causes the double delete of at least one static variable when the program finishes running. The vendor primarily targets windows and as in the referenced question, they use the appropriate dllexport attributes on that platform; I think they just missed it on linux and I don't expect it to be fixed. Is there something I can do when linking to their libraries to hide symbols then or is there some command I could run on their libraries to change the static variable names?
1 Answers
Instead of directly linking against the libraries, you could use dlopen
and dlclose
or their Windows equivalents (LoadLibrary
and FreeLibrary
).
With those functions, you have access to all the methods in the vendor-supplied libraries, but you get explicit control of when their initializers and deinitializers run.
So, on application exit you could unload one of the libraries, re-allocate the variable the other one is going to delete, and then let it get deleted a second time.
Or, you could force the application to close without running the second library's destructor at all (sending yourself a kill signal, aborting with _exit
, unregistering the finalizer, probably other ways...). Quoting from the atexit(3)
man page:
If one of the functions registered functions calls _exit(2), then any remaining functions are not invoked, and the other process termination steps performed by exit(3) are not performed.
You could even use the above atexit
hack to not have to use dlopen
- you can directly link the libraries.
As an alternative, you could locate the deinitializer from one of the loaded libraries in memory and rewrite it. I don't think that's a good idea, but it's technically possible.

- 95,191
- 9
- 106
- 122