Weird seg fault problem
<p>Greetings,</p>

<p>I'm having a weird seg fault problem. My application dumps a core file at runtime. After digging into it I found it died in this block:  </p>

<pre><code>#include &lt;lib1/c.h&gt;  
...  
x::c obj;  
obj.func1();  
</code></pre>

<p>I defined class c in a library lib1:  </p>

<pre><code>namespace x  
{  
    struct c  
    {  
        c();  
        ~c();  
        void fun1();  
        vector&lt;char *&gt; _data;  
    };  
}  

x::c::c()  
{  
}  

x::c::~c()  
{  
    for ( int i = 0; i &lt; _data.size(); ++i )  
        delete _data[i];  
}  
</code></pre>

<p>I could not figure it out for some time till I ran nm on the lib1.so file: there are more function definitions than I defined:  </p>

<pre><code>x::c::c()  
x::c::c()  
x::c::~c()  
x::c::~c()  
x::c::func1()  
x::c::func2()  
</code></pre>

<p>After searching in code base I found someone else defined a class with same name in same namespace, but in another library lib2 as follows:  </p>

<pre><code>namespace x  
{  
    struct c  
    {  
       c();  
       ~c();  
       void func2();  
       vector&lt;string&gt; strs_;  
    };  
}  

x::c::c()
{
}

x::c::~c()
{
}
</code></pre>

<p>My application links to lib2, which has dependency on lib1. This interesting behavior brings several questions:  </p>

<ol>
<li><p>Why would it even work? I would expect a "multiple definitions" error while linking against lib2 (which depends upon lib1) but never had such. The application seems to be doing what's defined in func1 except it dumps a core at runtime.  </p></li>
<li><p>After attaching debugger, I found my application calls the ctor of class c in lib2, then calls func1 (defined in lib1). When going out of scope it calls dtor of class c in lib2, where the seg fault occurs. Can anybody teach me how this could even occur?  </p></li>
<li><p>How can I prevent such problems from happening again? Is there any C++ syntax I can use?</p></li>
</ol>

<p>Forgot to mention I'm using g++ 4.1 on RHEL4, thank you very much!  </p>
