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?
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 isdeleted infoo‘s destructor.You might also want to change the contents of your
ifblock tothough the fact that you’re getting double frees here is indicative of larger issues than this.