Double free on app exit when mixing private and public linkages
When mixing public and private linkages with static and shared libraries, there is a specific dependency graph
that produces crashes on application exit while destroying static variables:
created: 0x55eae0647eb0
created: 0x55eae06482e0
destroying: 0x55eae06482e0
destroying: 0x55eae06482e0
free(): double free detected in tcache 2
I know that when two binaries (executable and shared library) link to the same static library you will get two instances of static variables from it, that's fine. But it shouldn't Crash.
The order of linkage to application matters. If first one linked is interface library - then it works fine and has only one instance of a static value from static library.
I don't know if that's a CMake or GCC bug. I assume it's a CMake bug, because if I make any change in dependency graph - the issue goes away.
It is reproduced on GCC 10.3.0, but doesn't reproduces on MSVC 14.29. CMake version is 3.22.1 (also tested on 3.21.1) I stripped out our project to make a minimal reproducible code with a test. It has 6 targets: 1 executable, 1 interface, 1 shared and 3 static. Sources at: https://github.com/VolodymyrZibarov/CMakePrivateLinkageBug