lundi 26 avril 2021

C++ object deletion crashing

I tried to include all necessary code that may be causing these problems. The program crashes the second time this code is run:

        for (int i = 0; i < player.projectiles.size(); i++)
        {
            bool temp = player.projectiles[i].move(begin, WIDTH);
            if (temp == true)
            {
                player.projectiles[i].destroy();
                player.projectiles.erase(player.projectiles.begin() + i);
            }
        }

Player.projectiles is a vector of objects from the class Projectiles. The member function move works perfectly and the function destroy looks like this:

void Projectile::destroy()
{
    delete this;
}

The first time the loop is run everything works fine, but the second time my program crashes and I can't figure out why. I suspect it has to do with deleting the object. Any help is greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire