Double deletion of static variable when linking two vendor-supplied libraries

I have a problem similar to this question: C ++ linux double destruction of a static variable. symbol anchors overlap, only my problem is with two vendor-supplied libraries, so I don't have access to the code. The vendor has two DLLs and linking to one of them works great, but linking to them will double delete at least one static variable when the program ends. The provider is primarily targeting windows, and as in the mentioned question, they use the appropriate dllexport attributes on that platform; I guess they just missed this on linux and I don't expect it to be fixed. Is there something I can do when referencing my libraries to hide the symbols, or is there some command I can run in my libraries to change the names of static variables?

+3


source to share


1 answer


Instead of a direct link with the libraries you can use dlopen

and dlclose

or their Windows equivalents ( LoadLibrary

and FreeLibrary

).

With these functions, you have access to all the methods in the vendor-supplied libraries, but you gain explicit control over when their initializers and deinitializers work.

So, when the application exits, you can unload one of the libraries, reallocate the variable that the other will delete, and then allow it to be deleted a second time.

Or you can force the application to close without running the second library destructor at all (by sending yourself a kill signal, aborting with _exit

, canceling the finalizer, perhaps in other ways ...). Quoting from the page atexit(3)

:



If one of the functions registered in the function calls _exit (2), then any other functions are not called, and the rest of the process termination steps performed by exit (3) are not executed.

You can even use the above atexit

hack to avoid using dlopen

- you can link libraries directly.

Alternatively, you can find the deinitializer from one of the loaded libraries in memory and rewrite it. I don't think this is a good idea, but it is technically possible.

0


source







All Articles