tags:

views:

144

answers:

3

Hi,

I have a code,

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

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

      foo& Foo()
      {
         return m_foo;
      }

  private:
      foo m_foo; 
};

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

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

   delete obj;
}

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

Please advice.

Thanks.

EDIT: Thanks for the comments but this is the exact code I used. Do you think the problem is in the foo& 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?

+1  A: 

Try running your code through Valgrind if you are using Linux.

Sam Miller
+5  A: 

There is no double free in the code you posted. I suspect that there is some pointer resource in foo, which is being copied as a pointer when foo is copied; yet is deleted in foo's destructor.

You might also want to change the contents of your if block to

foo& lcFoo = obj->Foo();

though the fact that you're getting double frees here is indicative of larger issues than this.

Billy ONeal
+4  A: 

Your foo class has not implemented the Rule of Three properly: every time you implement a destructor, it is usually an indication that you also need a copy constructor and copy assignment operator.

My guess is that you've got some pointer in the foo class that you're deleteing in foo's destructor, but since you're not implementing a copy constructor when you copy the foo class, it's just copying the pointer. When both instances of foo are destructed, they each try to free that pointer, and get the error.

However, rather than implementing a copy constructor and copy assignment operator, I would suggest you get rid of the destructor in foo and use a smart pointer (shared_ptr probably) instead.

As others have already pointed out, you probably also want to say foo &lcFoo = obj->Foo() but that's only going to hide the bigger problem.

Dean Harding
Use smart ptr instead of _what_ ? It only lets you to forget about `delete` ing, but does not replace destructor. I think you wanted to say `get rid of the delete` ?
Alexander Malakhov
I thought you don't need to impl any of the Three, when class is so simple, that you can just memcpy its instances
Alexander Malakhov
@Alexander: No, what he's posted is correct. The destructor is called as part of `delete`. +1 to this answer for indicating more specifically I meant by "larger issues than this" in my above answer.
Billy ONeal
@Billy I agree with him, but I just wanted to point out, that wording is confusing. Smart ptr can replace destructor only if object contains ptr, which isn't true in general case => hence confusion
Alexander Malakhov
@Alexander: Isn't that covered by "My guess is that you've got some pointer in the `foo` class" ?
Billy ONeal
@Billy: emmm... Now I finally see the point of yours and this answer, +1. A bit retarded at morning. Thanks for patience :)
Alexander Malakhov
Sorry @Alexander, yes @Billy is correct, I meant use a `smart_ptr` for the pointer that *I'm guessing* is contained in `foo`, rather than deleting it in `foo`'s destructor.
Dean Harding