1. Why or how does this piece of code “fix” the problem?
I'm not sure why, without breaking into the libstdC ++ code, but I assume that the memory allocated by the iostreams library, which is stored throughout the program, is reported as a valgrind problem when it is allocated in the shared library, but not when assigned to the main the program.
2. What equivalent, independent (from the standard library) code fragment provides the same error correction?
Firstly, I don’t know why you want something “independent of the standard library” when the available memory is still probably allocated by the standard library. The fix is either that you are not using the standard library at all, anywhere or using it differently.
Secondly, this “fix” is undefined behavior because you violate the rule of one definition by overriding std::ios_base differently to the correct definition in std lib.
The correct way to get the same behavior is #include <iostream> in your main.cpp file, including <iostream> defines a static std::ios_base::Init object. Alternatively, simply #include <ios> , and then define a static variable (but do not override the std::ios_base ), but basically what <iostream> does, so you can also use this.
source share