c++ - GCC __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) -


pursuant post, standalone functions/data in c++, proceeded put "common data" in anonymous namespace below , worked great on windows (vista 64 bit) on vs 2005/2008/2010

namespace {   ...   static std::string mystrings[] = {      str1,      str2,      ...,      strn   };   ... }  namespace mynamesp {    ...    use mystrings[] here..    ... } 

but on linux (so far tested rhel5 built gcc-4.1.2) promptly got segmentation fault.

$>myprog  segmentation fault $>gdb myprog  gnu gdb fedora (6.8-27.el5) copyright (c) 2008 free software foundation, inc. license gplv3+: gnu gpl version 3 or later <http://gnu.org/licenses/gpl.html> free software: free change , redistribute it. there no warranty, extent permitted law.  type "show copying" , "show warranty" details. gdb configured "x86_64-redhat-linux-gnu"... (gdb) r starting program: <path/to>/myprog  [thread debugging using libthread_db enabled] [new thread 0x2b8901a9da60 (lwp 32710)]  program received signal sigsegv, segmentation fault. 0x0000003e4ce9c928 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string ()    /usr/lib64/libstdc++.so.6 (gdb) bt #0  0x0000003e4ce9c928 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string ()    /usr/lib64/libstdc++.so.6 #1  0x00002b88ffde482b in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535)     @ <path/to>/mysource.cpp:140 #2  0x00002b88ffde4d65 in global constructors keyed _zn91_global__n__underscore_separated_path_to_mysource.cpp_00000000_6994a7da2_1e () @ <path/to>/mysource.cpp:12139 #3  0x00002b890011a296 in __do_global_ctors_aux ()    <path/to/libs>/debug/libmylibd.so #4  0x00002b88ffcd7f33 in _init () <path/to/libs>/debug/libmylibd.so #5  0x00002b8901672e40 in ?? () #6  0x000000326940d22b in call_init () /lib64/ld-linux-x86-64.so.2 #7  0x000000326940d335 in _dl_init_internal () /lib64/ld-linux-x86-64.so.2 #8  0x0000003269400aaa in _dl_start_user () /lib64/ld-linux-x86-64.so.2 #9  0x0000000000000001 in ?? () #10 0x0000000000000000 in ?? () (gdb) 

line 140 in backtrace call stack item #1 points end of array of strings definition. i've seen others error; no obvious fixes. appreciate thoughts/ideas/corrections always. thanks!

your problem releated static initialization order fiasco.

this happens when initialize static variable using static variable. when latter 1 has not been initialized yet, first 1 using non-initialized variable initialization.

the root cause order, in static variables initialized, undefined.

further reading: https://isocpp.org/wiki/faq/ctors#static-init-order

a typical workaround wrap static variables inside function. example:

t& getstatica() {   t static_var_a; // <--initialization here   return a; }  t static_var_b = getstatica(); // <-- static_var_a guaranteed initialized 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -