Rommel Alonzo
Rommel Alonzo

Reputation: 61

glibc detected double free or corruption

I'm having a glibc detected problem. I have researched a way to solve this however it seems like the correct solution is to free whatever I have allocated. However, even though I do this, I still receive a memory map print on my output screen.

class TestDepth{
    gameStatus temp;
public:
    TestDepth(gameStatus &currentGameState)
    {
        temp = currentGameState;

        free(&temp);
    }
};

I only get this error when I place the temp=currentGameState line. But once I comment that out, it works fine.

Upvotes: 0

Views: 891

Answers (3)

Sanish
Sanish

Reputation: 1719

temp = currentGameState;

This will call gameStatus's assignment operator(either default or your version). If the default assignment operator is called, then a bit wise assignment would have done. This is dangerous if currentGameState object has some heap resources in it. If there is an assignment operator for gameStatus, hopefully you have done the required deep copy.

Either way you are not supposed to use free to deallocate the object temp. One reason is that is not the right way to free an object. Another reason is this might try to free something which is not allocated by malloc.

Upvotes: 0

Rafael Baptista
Rafael Baptista

Reputation: 11499

You're not freeing what you think you are freeing. When you assign to temp you are assigning to a copy of gameStatus - which did not come from the heap. You are not deleting the version of GameStatus that is passed in as an argument, you are deleting a copy.

Skip the assignment to temp and just do

free( &currentGameState );

Upvotes: -1

David Hammen
David Hammen

Reputation: 33106

As you can see, I'm having a glibc detected problem. I have research a way to solve this however it seems like the correct solution is to free whatever I have allocated.

You aren't allocating anything. You shouldn't be freeing anything.

Upvotes: 5

Related Questions