Detect double free in C++ code
<p>I have a code,</p>

<pre><code>class foo
{
   public:
     foo(){};
     ~foo(){};
};

class bar
{
   public:
      bar(){};
     ~bar(){};

      foo&amp; Foo()
      {
         return m_foo;
      }

  private:
      foo m_foo; 
};

int main()
{
   bar *obj = new bar;

   if( /* true condition here */ )
   {
      foo lcFoo;
      lcFoo = obj-&gt;Foo(); // forgot this
   }

   delete obj;
}
</code></pre>

<p>Now I am getting a double free message from glibc when I call the "delete obj" part.</p>

<p>Please advice.</p>

<p>Thanks.</p>

<p>EDIT:
Thanks for the comments but this is the exact code I used. Do you think the problem is in the foo&amp; Foo() function because it return the reference of the m_foo object and then when code jump to the if condition brace then that m_foo will be deleted?</p>
