title: seg fault when using global object static member
body: <p>Can someone help understanding the following seg fautl, and also why it does work on changing the link order:</p>
  
  <p>common.h file:</p>
  
  <pre><code>#include &lt;set&gt;
  struct T {
      explicit T(const char*) {
          Instances.insert(this);
      }
      static std::set&lt;T*&gt; Instances;
  };
  </code></pre>
  
  <p>d.cc file:</p>
  
  <pre><code>#include "common.h"
  T d(__FILE__);
  </code></pre>
  
  <p>main.cc file:</p>
  
  <pre><code>#include "common.h"
  #include &lt;set&gt;
  #include &lt;iostream&gt;
  
  /*static*/ std::set&lt;T*&gt; T::Instances;
  int main() {
     std::cout &lt;&lt; "T::Instances.size() = " &lt;&lt; T::Instances.size() &lt;&lt; std::endl;
     while(true);
  }
  </code></pre>
  
  <p>Building and running with following commands:</p>
  
  <pre><code>g++ -c -g -Wall -Wextra d.cc -o d.o
  g++ -c -g -Wall -Wextra main.cc -o main.o
  g++  -g  d.o main.o -o app
  </code></pre>
  
  <p>./app</p>
  
  <p>Running the commands I get a seg fault, with the following backtrace:</p>
  
  <pre><code>$ ./app
  Segmentation fault (core dumped)
  $ gdb ./app
  (gdb) run
  Starting program: /home/meodou/zdev/poc_at_7405/testi/app 
  Program received signal SIGSEGV, Segmentation fault.
  0x00007ffff7afebca in std::local_Rb_tree_decrement (__x=0x6031e8 &lt;T::Instances+8&gt;) at ../../../../../libstdc++-v3/src/c++98/tree.cc:98
  98          &amp;&amp; __x-&gt;_M_parent-&gt;_M_parent == __x)
  (gdb) bt
  #0  0x00007ffff7afebca in std::local_Rb_tree_decrement (__x=0x6031e8 &lt;T::Instances+8&gt;) at ../../../../../libstdc++-v3/src/c++98/tree.cc:98
  #1  0x0000000000401365 in std::_Rb_tree_iterator&lt;T*&gt;::operator-- (this=0x7fffffffdc50) at /usr/include/c++/5.3.1/bits/stl_tree.h:220
  #2  0x000000000040102f in std::_Rb_tree&lt;T*, T*, std::_Identity&lt;T*&gt;, std::less&lt;T*&gt;, std::allocator&lt;T*&gt; &gt;::_M_get_insert_unique_pos (
  this=0x6031e0 &lt;T::Instances&gt;, __k=@0x7fffffffde08: 0x6031d1 &lt;d&gt;) at /usr/include/c++/5.3.1/bits/stl_tree.h:1819
  #3  0x0000000000400dfe in std::_Rb_tree&lt;T*, T*, std::_Identity&lt;T*&gt;, std::less&lt;T*&gt;, std::allocator&lt;T*&gt; &gt;::_M_insert_unique (this=0x6031e0 &lt;T::Instances&gt;, 
  __v=@0x7fffffffde08: 0x6031d1 &lt;d&gt;) at /usr/include/c++/5.3.1/bits/stl_tree.h:1863
  #4  0x0000000000400d79 in std::set&lt;T*, std::less&lt;T*&gt;, std::allocator&lt;T*&gt; &gt;::insert (this=0x6031e0 &lt;T::Instances&gt;, __x=@0x7fffffffde08: 0x6031d1 &lt;d&gt;)
  at /usr/include/c++/5.3.1/bits/stl_set.h:485
  #5  0x0000000000400d47 in T::T (this=0x6031d1 &lt;d&gt;) at common.hh:6
  #6  0x0000000000400ce0 in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) at d.cc:2
  #7  0x0000000000400d0a in _GLOBAL__sub_I_d () at d.cc:2
  #8  0x0000000000401b2d in __libc_csu_init ()
  #9  0x00007ffff71a050f in __libc_start_main (main=0x40173b &lt;main()&gt;, argc=1, argv=0x7fffffffdf68, init=0x401ae0 &lt;__libc_csu_init&gt;, fini=&lt;optimized out&gt;, 
  rtld_fini=&lt;optimized out&gt;, stack_end=0x7fffffffdf58) at libc-start.c:245
  #10 0x0000000000400bc9 in _start ()
  </code></pre>
  
  <p>So that something I m trying to understand.
  The second thing is that if I invert the linking order:</p>
  
  <pre><code>From  g++  -g  d.o main.o -o app
  To  g++  -g  main.o d.o -o app
  </code></pre>
  
  <p>The program runs without seg fault.</p>
  
  <pre><code>$ g++ -c -g -Wall -Wextra d.cc -o d.o   
  $ g++ -c -g -Wall -Wextra main.cc -o main.o
  $ g++  -g  main.o d.o -o app
  $ ./app
  T::Instances.size() = 1
  </code></pre>
  
  <p>Any explanation what this is working? The problem seems to be related to global variables initialization but I still cannot see what is happening.</p>
  
  <p>Using g++ (GCC) 5.3.1</p>
  
  <p>BR,</p>
  
  <p>...........................</p>
  
  <p>In answer to Quentin comment:
  Yes I know that is should be related to static initialization  but still I dont see really how this is happening. (I am not in the case of <a href="https://isocpp.org/wiki/faq/ctors#static-init-order" rel="nofollow">https://isocpp.org/wiki/faq/ctors#static-init-order</a> where a compile unit is calling an object not initialized as it s found in another compile unit, at least not directly).</p>
  
  <p>I can also add a T object the main.cc file:</p>
  
  <pre><code>#include &lt;iostream&gt;
  /*static*/ std::set&lt;T*&gt; T::Instances;
  +T main_obj(__FILE__);
  int main() {
  std::cout &lt;&lt; "T::Instances.size() = " &lt;&lt; T::Instances.size() &lt;&lt; std::endl;
  </code></pre>
  
  <p>I ll still get a crash, here my understanding is that we are calling a static function on an object that we already have in current compile unit (should be correctly initialized). So we are not depending at least directly on the object found in d.cc file.
  Again I strongly believe that is related to static initialization but I dont clearly see the explanation of it. (The object that we are calling its static member should be already initialized)</p>