Jump to content
  • Advertisement
Sign in to follow this  
Mr_Threepwood

C++ vector problem

This topic is 3964 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

Hi, so I'm reading "Beginning Game Programming" by Michael Morrison, and so far it's pretty good. However there's a part of his code that is giving me problems and I tried to fix it but got stuck. The part of the code giving me problems is this:
// 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--;
  }
The code itself compiles, but when I try to close the program I get an error: --------------------------- Microsoft Visual C++ Debug Library --------------------------- Debug Assertion Failed! Program: ..._Threepwood\My Documents\Chap10\Planets 2\Debug\Planets 2.exe File: c:\program files\microsoft visual studio 9.0\vc\include\vector Line: 138 Expression: ("this->_Has_container()", 0) For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts. (Press Retry to debug the application) --------------------------- Abort Retry Ignore --------------------------- I checked line 138 of the file it mentions, and that is where it is dealing with the -- operator, so I'm pretty sure that's what's causing the problem. Does anyone know why this problem occurs and how to fix it?

Share this post


Link to post
Share on other sites
Advertisement
If you decrement a vector iterator when it is the beginning iterator, that will cause in assertion error. Since you are just deleting everything in the vector, I don't know why you couldn't just do something like:

for (vector<Sprite*>::iterator siSprite = m_vSprites.begin(); siSprite != m_vSprites.end(); siSprite++)
delete (*siSprite);

m_vSprites.clear();

Share this post


Link to post
Share on other sites
For future reference, the error occurred because when you call erase() on a iterator, all iterators to the vector are invalidated. std::vector::erase returns a valid iterator. With this, you could re-write the loop but it is more complex (you must not increment the iterator returned by erase or you will skip elements).

One solution is to use boost::ptr_vector, if there are no ownership issues (i.e. the array owns the objects). Otherwise, a std::vector of boost::shared_ptr usually suffices (circular references excluded).

A nice solution is to use some of the standard library algorithms.

#include <algorithm>

struct Deleter {
template< typename T >
void operator()( T *pointer ) { delete pointer; }
};

template< typename Container >
void clearPtrContainer(Container &c)
{
std::for_each(c.begin(),c.end(),Deleter());
c.clear();
}

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!