On Linux I have some generated C++ code from a static library that defines a global variable. A single instance of this global variable is shared between two shared libraries that refer to its symbol.
When the process shuts down and the static termination phase is run, I see that the destructor on this shared instance is run twice! Presumably once per library as each unloads.
This question is closely related to another I saw recently here: related question. This sounds like the same behavior, but there is no discussion about why it is happening.
Does anybody know the theoretical explanation behind this behavior?
If you take a naked pointer and place it in a smart pointer (twice), it will destruct twice, once for each container refcount falling to zero.
So, if you pass the naked pointer into both libraries, that would do it. Each one puts it in a shared pointer object and each of those does the destruction. If you can see the stack backtrace during the dtor, that should show it happening in both of the libraries.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With