removing sprite from memory

Started by
13 comments, last by PezMutanT 15 years, 12 months ago
Quote:Original post by Gage64
Sorry for sort of dumping all this information on you. If you have any specific questions, I'll try to explain further.


By any chance you have to be sorry, I'm very grateful for this info about the observer pattern and I am going to read about it because I want to learn how to implement it (that's the only way to learn!). I think this observer pattern should be a better approach to what I had in mind. Thanks a lot!

Quote:Original post by Gage64
BTW, your loop doesn't work properly. When an object is deleted, you go back to the beginning of the loop, thus updating some objects more than once. Instead, you can take advantage of the fact that erase() returns an iterator to the next object after the deleted one.


I returned to the beginning because I was getting errors when deleted an element and this was the only way I could make it work. Thanks a lot for your suggestion, I will try your way, it makes more sense!

Thanks! ;)
Advertisement
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());
bool isDeadSprite(Sprite*& s) {    return s->IsKilled() { delete s; return true; }    return false;}


This syntex looks strange (the second line). Assuming it's not a typo, can you please explain how it works?
Quote:Original post by Gage64
bool isDeadSprite(Sprite*& s) {    return s->IsKilled() { delete s; return true; }    return false;}


This syntex looks strange (the second line). Assuming it's not a typo, can you please explain how it works?
I'm guessing that was supposed to be:
if (s->IsKilled()) { delete s; return true; }
Could be wrong though.
Thanks for your answers.

I already tried having a function designed only for checking every sprite and removing if it "IsKilled". But I still would have the same problem: I have to delete it in two different vectors (the one with all the sprites and the one in the main file with the enemy sprites), because otherwise I don't know how to check in the main loop if all enemies are dead.

The only thing I could think of is not deleting sprites until the end of the stage, simply "stop drawing" the killed ones, but I think this is an awful way to do it because of the memory wasting. I would like to be able to delete a sprite when it is killed, I think the observer pattern is a better approach to my idea.

Thanks a lot people! ;)

This topic is closed to new replies.

Advertisement