全局变量相互依赖和初始化顺序的解决办法

时间:2021-01-04 19:42:30

如果是定义一个全局的map,会出现如下core:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b449ea in std::_Rb_tree_decrement(std::_Rb_tree_node_base*) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
(gdb) bt
#0  0x00007ffff7b449ea in std::_Rb_tree_decrement(std::_Rb_tree_node_base*) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x0000000000404071 in std::_Rb_tree_iterator<std::pair<int const, int> >::operator-- (this=0x7fffffffe330) at /usr/include/c++/4.6/bits/stl_tree.h:203
#2  0x00000000004037b2 in std::_Rb_tree<int, std::pair<int const, int>, std::_Select1st<std::pair<int const, int> >, std::less<int>, std::allocator<std::pair<int const, int> > >::_M_insert_unique (this=0x607280, __v=...) at /usr/include/c++/4.6/bits/stl_tree.h:1287
#3  0x000000000040331b in std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > >::insert (this=0x607280, __x=...)
    at /usr/include/c++/4.6/bits/stl_map.h:518
#4  0x0000000000402818 in ArgsParser::register_arg (param_name=..., arg_info=0x6080a0) at ./args_parser.cpp:130
#5  0x00000000004017d6 in util::CArgInfo<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::CArgInfo (this=0x6080a0, param_name=...)
    at /data1/mooon/run/include/util/args_parser.h:307
#6  0x00000000004016b3 in util::CStringArgInfo<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::CStringArgInfo (this=0x6080a0, 
    optional=false, param_name=..., default_value=..., help_string=...) at /data1/mooon/run/include/util/args_parser.h:392
#7  0x000000000040143d in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) at x.cpp:7
#8  0x0000000000401523 in _GLOBAL__sub_I__ZN10ArgsParser2ipE () at x.cpp:12
#9  0x0000000000404a8d in __libc_csu_init ()
#10 0x00007ffff7528700 in __libc_start_main (main=0x401374 <main()>, argc=1, ubp_av=0x7fffffffe6a8, init=0x404a30 <__libc_csu_init>, fini=<optimized out>, 
    rtld_fini=<optimized out>, stack_end=0x7fffffffe698) at libc-start.c:185
#11 0x00000000004012b9 in _start ()

问题弄清楚了,是因为全局变量相互依赖的问题,被依赖的晚于初始化造成的(如全局对象A依赖于全局对象B,而B晚于A被构造出来)这和编译器版本有关,因为之前一直未发现这个问题。解决办法是,将被依赖的改成局部静态变量,通过函数方式引用,如:

原来为:static string g_info;
改成:
string& info()
{
    static string s_info; // 局部静态对象,只有在使用到时才会被构造出来,由于是在全局对象构造中被调用到,所以不存在多线程问题,因为这发生在main函数之前,其它线程还没有创建出来,如果这个时候就有线程了,那就考虑是否设计合理。
    return s_info;
}

问题就可以解决了。