   Deleting from a std::set
  When i started making the physics system for my game a few months ago, i decided to handle the removal of objects from the system by keeping a separate list of "to remove" objects:// pseudocodestd::set<PhysicsObject*> objects;std::set<PhysicsObject*> objectsToRemove;...std::set<PhysicsObject*>::iterator i;for (i = objects.begin(); i != objects.end(); i++) {    (*i)->update(t);}if (!objectsToRemove.empty()) {    for (i = objectsToRemove.begin(); i != objectsToRemove.end(); i++) {   objects.erase(i);    }    objectsToRemove.clear();}I did this mainly because i didn't want to use a "to remove" flag in each object and do a check each iteration. There was a thread here debating what the best OO way to do this is but i can't find it (perhaps someone knows).But the other day i realised that way seemed over-complicated and decided it would be easier and maybe even a bit faster to use a boolean flag. The only problem is that i can't just delete it right there on the spot because that would invalidate the iterator. So i wrote the code below.bool anyToRemove = false;PhysicsObject* obj = NULL;std::set<PhysicsObject*>::iterator i;for (i = objects.begin(); i != objects.end(); i++) {    obj = *i;    obj->updatePhysics(t);    if (obj->canRemoveFromPhysics())   anyToRemove = true;}if (anyToRemove) {    for (i = objects.begin(); i != objects.end(); i++) {   if ((*i)->canRemoveFromPhysics()) {  // delete *i;// Although i don't think i want to delete it  objects.erase(i);  i++;   }    }}But this doesn't work either. I realised after i wrote it that i probably don't need the second loop and could do it all in one but i get some runtime errors.When i tried to delete the object i get:"Assertion failed. Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)"Which is coming from the destructor.But i realised i don't want to delete the object, that will be done elsewhere. So i removed that code but now i get another error:"map/set iterator not incrementable"When calling i++.I am not a complete n00b at C++ but i don't really understand STL all that much. Does anyone know what i am doing wrong?thanks   
  Your removal loop should look like:    for (i = objects.begin(); i != objects.end();) {   if ((*i)->canRemoveFromPhysics()) {  objects.erase(i++);   } else {  ++i   }    };if (!objectsToRemove.empty()) {    for (i = objectsToRemove.begin(); i != objectsToRemove.end(); i++) {Don't do that. Zero is not a special case. If the container is empty, then its .begin() equals its .end() (that's why you loop while '!= end'; the .end() iterator does not actually reference an element), and the loop correctly iterates zero times.After all, you'd never writeif (x != 0) {    for (int y = 0; y < x; ++y) {right?   ;Quote:Original post by SiCraneYour removal loop should look like:...Thanks SiCrane, that makes sense now. The problem was that i was incrementing twice when i removed an object, right?One other thing, why do you do ++i rather than i++? Both are equivalent as long as the result isn't being assigned to anything aren't they?Quote:Original post by ZahlmanDon't do that. Zero is not a special case...Oh, of course [embarrass]. I guess i wasn't thinking very hard when i wrote that code. Since this is my first game and i am doing it in my spare time i am not quite sure of how to do the physics system. Although the code i posted isn't specific to physics simulation, not having a clear design plan obviously degrades programming performance.Anyway, SiCrane's code works so i will just stick with that and move on.thanks guys.   ;Quote:Original post by XTAL256Thanks SiCrane, that makes sense now. The problem was that i was incrementing twice when i removed an object, right?That was one problem. The other problem was that you were trying to increment an invalidated iterator.Quote:One other thing, why do you do ++i rather than i++? Both are equivalent as long as the result isn't being assigned to anything aren't they?No, ++i is generally less expensive than i++.   