void GameEngine::CleanupSprites()
{
// Delete and remove the sprites in the sprite vector
vector<Sprite*>::iterator siSprite;
for (siSprite = m_vSprites.begin(); siSprite != m_vSprites.end(); siSprite++)
{
delete (*siSprite);
m_vSprites.erase(siSprite);
siSprite--;
}
}
VC 2005 express and the stl vector class
I just moved from VC6 to the free VC8. I have downloaded and installed the platform sdk. I believe I have configured everything correctly. I can create a sample win32 program and it runs fine. Now I am trying to recreate some of my old programs from VC6. I'm running into a problem with the vector class. The game runs fines, but if I close the game it crashes. I've narrowed the problem down to the function that cleans up all the sprites. I store the sprites in a vector. Here is the function
Is there a problem with accessing the first member in a vector? The program seems to crash only when the 1st member is deleted.
I am using cpp, and this program worked fine in VC6. Any ideas?
Your code is non-kosher iterator usage. Decrementing a vector iterator that points to the first element is not valid. Consider calling delete on all the elements and then calling clear() rather than erasing as you delete.
Quote:Original post by cmptrgear
Doesnt erase also invalidate the iterator?
In a vector, yes, but it does return the next iterator.
By the way, per SiCrane's suggestion [edit: first version would not call destructors, you could call them in a second pass, I suppose, but this is better]:
[edit2: An even better solution would be to use boost::ptr_vector, or boost:shared_ptr with std::vector ]
#include <algorithm>#include <functional>// ...void Deleter( Sprite const * const p ) { delete p;}void GameEngine::CleanupSprites(){ std::for_each( m_vSprites.begin(), m_vSprites.end(), Deleter ); m_vSprites.clear();}
[edit2: An even better solution would be to use boost::ptr_vector, or boost:shared_ptr with std::vector ]
Quote:Original post by Anonymous Poster
VC 6's STL implementation is horribly broken. Try STL Hack.
Of course if you had actually read his post you would realise that he's using VC8 now, and it's STL implementation is just fine.
Quote:Original post by joanusdmentiaQuote:Original post by Anonymous Poster
VC 6's STL implementation is horribly broken. Try STL Hack.
Of course if you had actually read his post you would realise that he's using VC8 now, and it's STL implementation is just fine.
Or even just the title. And it's STLPort.
Thanks for the advice. I altered the cleanup sprites function. Now it just deletes the sprite, then clears the entire vector after.
Alternativly I could have done this right?
The other problem I had was during the sprite update function. When a sprite is marked as dying, it is removed from the vector inthe same manner as before.
This worked only untill the 1st element in the vector was deleted. It was said that erase() returned the next iterator, I commented out the siSprite--, and now it works fine. Is this correct or am I just lucky it worked out this time?
for (siSprite = m_vSprites.begin(); siSprite != m_vSprites.end(); siSprite++) { delete (*siSprite); } m_vSprites.clear();
Alternativly I could have done this right?
for_each(m_vSprites.begin(),m_vSprites.end(),delete (*siSprite) )
The other problem I had was during the sprite update function. When a sprite is marked as dying, it is removed from the vector inthe same manner as before.
// Kill the sprite delete (*siSprite); m_vSprites.erase(siSprite);// siSprite--; continue;
This worked only untill the 1st element in the vector was deleted. It was said that erase() returned the next iterator, I commented out the siSprite--, and now it works fine. Is this correct or am I just lucky it worked out this time?
Quote:Original post by localrob2
Alternativly I could have done this right?
*** Source Snippet Removed ***
time?
No.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement