Jump to content
  • Advertisement
Sign in to follow this  
localrob2

VC 2005 express and the stl vector class

This topic is 4422 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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--;
  }
}
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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by cmptrgear
Doesnt erase also invalidate the iterator?

In a vector, yes, but it does return the next iterator.

Share this post


Link to post
Share on other sites
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]:
#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 ]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
VC 6's STL implementation is horribly broken. Try STL Hack.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by joanusdmentia
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.


Or even just the title. And it's STLPort.

Share this post


Link to post
Share on other sites
Thanks for the advice. I altered the cleanup sprites function. Now it just deletes the sprite, then clears the entire vector after.

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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by localrob2
Alternativly I could have done this right?
*** Source Snippet Removed ***
time?

No.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!