title: segmentation fault while using dynamically allocated object in shared lib loaded at runtime
body: <p>I have static library linked to executable file. Executable file itself don't use library symbols. But this executable loads some shared libraries at runtime, one of which uses symbols from library. Below is very simplified version of library source files.</p>
  
  <p>ParentClass.h</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  class ParentClass {
    ParentClass() {}
  
   // some functionality
   };
  </code></pre>
  
  <p>ChildClass.h</p>
  
  <pre><code>#include &lt;ParentClass.h&gt;
  
  struct StaticData {
    static const char *staticString;
  };
  
  class ChildClass : public ParentClass, public StaticData {
    ChildClass() {}
    // some extended functionality here
  };
  </code></pre>
  
  <p>ChildClass.cpp</p>
  
  <pre><code>#include "ChildClass.h"
  
  const char * StaticData::staticString = "string";
  
  // functionality implementation
  </code></pre>
  
  <p>Here is several facts:</p>
  
  <p>1.code like this:</p>
  
  <pre><code>ChildClass test;
  
  //extended use of test functionality
  </code></pre>
  
  <p>works quite well.</p>
  
  <p>2.code like this:</p>
  
  <pre><code> ChieldClass *test = new ChieldClass();
      test-&gt;some_func(); // some func don't use dynamic memory
      test-&gt;some_other_func(); // dynamic memory used (in my case malloc in gethostbyname system function)
  </code></pre>
  
  <p>works quite well when used in binary directly linked with library, but fails with segfault <code>"path_to_exec malloc(): memory corruption: some_address"</code> when used in shared library loaded in runtime (see description in the beginning).</p>
  
  <p>3.code like this:</p>
  
  <pre><code>ParentClass *test = new ParentClass();
  test-&gt;some_func();
  test-&gt;some_other_func();
  </code></pre>
  
  <p>Works well everywhere. </p>
  
  <p>I'm having trouble understanding why code in item 2 causes segmentation fault, but I am suspecting that trouble is in the use of static data in ChildClass (besides that difference ChildClass only defines some new functions with extended functionality that uses ParenClass functions. Segmentation fault occures even when I am using not overloaded ParentClass functions). But I can't link this single difference with the fact that segnemtation fault occures only while using ChildClass in shared library dynamically loaded to executable my library was linked with.</p>
  
  <p>I'l be glad to hear any ideas to get rid of this seg fault.</p>
  
  <p>Update: bt when using logger function with std::cout (some names are omitted). Call sequence:</p>
  
  <pre><code>ChildClass *test = new ChildClass();
  test-&gt;printInfo();
  test-&gt;connect();
  </code></pre>
  
  <p>connect fnction isn't redefined in ChildClass.</p>
  
  <pre><code>(gdb) bt
  #0  0x00007f756f67e165 in raise () from /lib/libc.so.6
  #1  0x00007f756f680f70 in abort () from /lib/libc.so.6
  #2  0x00007f756f6b427b in ?? () from /lib/libc.so.6
  #3  0x00007f756f6bdad6 in ?? () from /lib/libc.so.6
  #4  0x00007f756f6c0b6d in ?? () from /lib/libc.so.6
  #5  0x00007f756f6c2930 in malloc () from /lib/libc.so.6
  #6  0x00007f756f6af35b in ?? () from /lib/libc.so.6
  #7  0x00007f756f7291de in ?? () from /lib/libc.so.6
  #8  0x00007f756f72aa65 in __res_maybe_init () from /lib/libc.so.6
  #9  0x00007f756f72ca70 in __nss_hostname_digits_dots () from /lib/libc.so.6
  #10 0x00007f756f731fe4 in gethostbyname_r () from /lib/libc.so.6
  #11 0x0000000000507929 in underlaying_c_code_connect (client=0x7f7564017348) at /home/beduin/???/lib/???/UnderlayingCCode.cpp:1477
  #12 0x0000000000504a24 in ParentClass::connect (this=0x7f7564017340) at /home/beduin/???/lib/???/ParentClass.cpp:216
  #13 0x00007f7569342f68 in Plugin::Start (this=0x7f75640208c0) at /home/beduin/???/plugins/???/Plugin.cpp:84
  #14 0x00000000004c7d45 in ???::PluginHolder::StartPlugin (this=0x7fffed7dc5e0, it=@0x7fffed7dbad0) at /home/beduin/???/plugins.cpp:317
  #15 0x00000000004c8656 in ???::PluginHolder::Start (this=0x7fffed7dc5e0) at /home/beduin/mrvs/framework/base/plugins.cpp:401
  #16 0x00000000004c7935 in ???::PluginHolder::LockNLoad (this=0x7fffed7dc5e0) at /home/beduin/???/plugins.cpp:284
  #17 0x00000000004afe6f in main (argc=3, argv=0x7fffed7dd978) at /home/beduin/???/main.cpp:148
  </code></pre>
  
  <p>Using custom logger:</p>
  
  <pre><code>#0  0x00007f824aa12165 in raise () from /lib/libc.so.6
  #1  0x00007f824aa14f70 in abort () from /lib/libc.so.6
  #2  0x00007f824aa4827b in ?? () from /lib/libc.so.6
  #3  0x00007f824aa51ad6 in ?? () from /lib/libc.so.6
  #4  0x00007f824aa54b6d in ?? () from /lib/libc.so.6
  #5  0x00007f824aa56930 in malloc () from /lib/libc.so.6
  #6  0x00007f824b2a46bd in operator new () from /usr/lib/libstdc++.so.6
  #7  0x00007f824b280b29 in std::string::_Rep::_S_create () from /usr/lib/libstdc++.so.6
  #8  0x00007f824b281aeb in std::string::_Rep::_M_clone () from /usr/lib/libstdc++.so.6
  #9  0x00007f824b28205c in std::string::reserve () from /usr/lib/libstdc++.so.6
  #10 0x00007f824b27c021 in std::basic_stringbuf&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::overflow () from /usr/lib/libstdc++.so.6
  #11 0x00007f824b280215 in std::basic_streambuf&lt;char, std::char_traits&lt;char&gt; &gt;::xsputn () from /usr/lib/libstdc++.so.6
  #12 0x00007f824b2763b5 in std::__ostream_insert&lt;char, std::char_traits&lt;char&gt; &gt; () from /usr/lib/libstdc++.so.6
  #13 0x00007f824b27662f in std::operator&lt;&lt; &lt;std::char_traits&lt;char&gt; &gt; () from /usr/lib/libstdc++.so.6
  #14 0x00000000004f4fb0 in ???::Logger::LogWriter::operator&lt;&lt; &lt;char [25]&gt; (this=0x7fff8e241fc0, str=@0x52b2fd)
      at /home/beduin/???/log:184
  #15 0x0000000000500388 in ChildClass::printInfo (this=0x7f8240017470) at /home/beduin/???/ChildClass.cpp:480
  #16 0x00007f82446d6f5c in Plugin::Start (this=0x7f82400208a0) at /home/beduin/???/plugins/???/Plugin.cpp:83
  #17 0x00000000004c7d35 in ???::PluginHolder::StartPlugin (this=0x7fff8e243b30, it=@0x7fff8e243020) at /home/beduin/???/plugins.cpp:317
  #18 0x00000000004c8646 in ???::PluginHolder::Start (this=0x7fff8e243b30) at /home/beduin/???/plugins.cpp:401
  #19 0x00000000004c7925 in ???::PluginHolder::LockNLoad (this=0x7fff8e243b30) at /home/beduin/???/plugins.cpp:284
  #20 0x00000000004afe5f in main (argc=3, argv=0x7fff8e244ec8) at /home/beduin/???/main.cpp:148
  </code></pre>