I am trying to debug a C ++ program compiled with GCC that crashes on startup. The GCC mutex protects the function of static local variables, and it seems that waiting for such a lock to happen causes it to hang. How this happens is rather confusing. The first module Static initialization occurs (there are __static_init functions called by GCC that are visible in backtrace), which calls the Foo () function, which has a static local variable. A static local variable is an object that the constructor calls through several levels of functions, and then the backtrace has several characters, and then it is in the static initialization of the second module B (the __static functions happen again), which then calls Foo (), but since Foo () never returned the first time the mutex on the local static variable is still set, and it is locked.
How does one static init trigger another? My first theory was a shared library - this module A would call some function in module B that would load module B, thereby causing static initialization of B, but this does not seem to be the case. Module A does not use module B at all. Therefore, I have a second (and terrifying) hunch. Say that:
Module A uses some template function or function in the template class, for example. foo<int>::bar()
Module B also uses foo<int>::bar()
Module A is completely independent of module B
At the time of the link, the linker has two instances of foo<int>::bar() , but this is normal, because the template functions are marked as weak characters ...
At run time, module A calls foo<int>::bar , and the static init of module B starts, although module B is independent of module A! What for? Since the linker decided to go with the instance of module B foo :: bar instead of the instance of module A during the connection.
Is this particular scenario valid? Or does one static init module never run static init in another module?
Explanation: GCC automatically creates mutexes to protect any static function variable. I do nothing with mutexes. This is a GCC method for providing robust function of static variables.
Update . I know that static initialization is not defined between translation units and that I should not depend on the order. But I wonder if this is normal behavior, as the key to debugging the problem. Is it normal for the compiler to generate code that does this, or does it potentially indicate an error in GCC?
source share