Quote:Original post by PezMutanT
Quote:Original post by Gage64
It's not NULL, but that shouldn't matter. Once you delete an object through a pointer, then as far as you're concerned, the object no longer exists. Attempting to use the object through the pointer will result in undefined behavior (most likely a crash).
Also, I'm not sure how this is related to your problem. How are you testing to see if a sprite is alive?
I'm checking it checking the "IsKilled" boolean property of the object, but I don't access the object with the pointer I deleted (the one in the vector with all the sprites), I do it through the pointer in the vector with all the enemies to see if there's any enemy alive yet (and that pointer is pointing to the same sprite that has been deleted).
Sigh.
'delete'
does not have any effect on the pointer it's called upon. It affects the pointed-at thing. "The pointer you deleted" is actually the pointed-at thing you deleted. The pointer in the vector pointed to the same thing as the pointer you used for the delete statement. It now points to garbage, in the same way that the other pointer does, because the pointed-at thing does not exist any more.
Solution: don't delete when you set IsKilled. Just set it. Then have a separate pass that goes through the vector, to delete
and remove all the pointers to IsKilled sprites.
void Sprite::update() { // Among other wonderful things: if (blarg_im_ded) { IsKilled = true; }}// We need a little adaptor function that will delete the pointers as a part of// the process of checking for dead Sprites:bool isDeadSprite(Sprite*& s) { return s->IsKilled() { delete s; return true; } return false;}// Then, assuming we have:std::vector<Sprite*> allSprites;// Update everything like:std::for_each(allSprites.begin(), allSprites.end(), std::mem_fun(&Sprite::update));// Clean up the corpses like:allSprites.erase(std::remove_if(allSprites.begin(), allSprites.end(), isDeadSprite), allSprites.end());