Once you erase() something from a vector, all existing iterators of that vector become invalid.
That means:
if (DetectCollision.BoundingBoxTest((*iterP)->getSprite(), (*iterA)->getSprite()))
{
//...
pProjectileVector->erase(iterP); // iterP is no longer valid after this call
//...
iterP--; // this is meaningless. Next loop iteration, iterP will be bad/invalid
// same problem with iterA
iterP--; is a bad idea anyway. A general rule of thumb is that if you need to have something that "undoes" the for loop increment, then you probably shouldn't be using a for loop.
To get a valid iterator after an erase, use erase's return value.
This code should work (untested):
vector<Projectile*>::iterator iterP = pProjectileVector->begin();
while( iterP != pProjectileVector->end() )
{
vector<Asteroid*>::iterator iterA = pAsteroidVector->begin();
while( (iterA != pAsteroidVector->end()) && (iterP != pProjectileVector->end()) ) // have to check iterP in this loop, too
// since it could reach the end of the vector as well
{
if (DetectCollision.BoundingBoxTest((*iterP)->getSprite(), (*iterA)->getSprite()))
{
delete *iterP;
iterP = pProjectileVector->erase(iterP); // iterP =
delete *iterA;
iterA = pAsteroidVector->erase(iterA); // iterA =
Asteroids.setAsteroidCount(Asteroids.getAsteroidCount()-1);
}
else // no collision
{
++iterA;
}
}
if(iterP != pProjectileVector->end())
++iterP;
}