mercredi 1 novembre 2017

Weird heap corruption when deleting a class pointer in some way

So... I'm coding a game in c++ with visual studio and I have a weird problem with heap corruption.

Basically I have a system where a object Guy can grab Raw resources, place them in a spot, and build a Block with those resources. When a Block is built all the resources used to make it are deleted from the game. I also have a debug menu where I can choose to spawn what object I want included Blocks. Finally I can click to any object and choose to delete them manually.

All the object management is made with pointers and the code used to delete them is basically the same between the manual interface and the automatic deleting when building an object.

Manual delete:

void Button_Destroy_Gameobject(WindowObject* wo) {
    /*Non relevant stuff here*/
    delete wo->GetObjectPointer();
}

Automatic delete through build process(I make a ghost to order the guy to make the block, then when the ghost is built I replace it with the actual block and delete it so this deconstructor is called):

Gameobject_ghost::~Gameobject_ghost() {
    /*Non relevant stuff here similar to before*/
    /*To this point I breakpointed and check that all the pointers in the vector are valid pointers and they point to the right objects*/
    for (unsigned i = 0; i < objectsStored_.size(); i++) {
        delete objectsStored_[i];
    }
}

In the previous codes wo->GetObjectPointer() and objectStored_[i] points at the same memory location.

The test scene starts blank with a Guy, I spawn in the same way 5 Raw resources and I place the order to build a Block. I manually delete one Raw resource to check that those pointers can be correctly be deleted and it delete it correctly without heap corruption. Then I order the Guy to build a Block with the remaining 4 Raw resources. He starts the job and when he has to complete the building process the game crashes returning heap corruption when trying to delete the first object stored in the ghost.

HEAP CORRUPTION DETECTED: after Normal block (#16599) at 0x0000025CA1DADDE0.
CRT detected that the application wrote to memory after end of heap buffer.

I hope that someone can fix my problem or at least suggest me something to do to better check for a solution.

Aucun commentaire:

Enregistrer un commentaire