Original Post
Hi everyone, I've been working on a 2D shooter for fun and to improve my C++, but I've got to a part where I'm struggling.
Whenever a bullet goes offscreen or hits an enemy the game freezes with processor usage at 100%. Here's my code for managing sets of bullets, it's where things seem to be going wrong:
void unitHandler::bullets(deque<boost::shared_ptr<Bullet> >& bulletPool){ for(deque<boost::shared_ptr <Bullet> >::iterator iter = bulletPool.begin(); iter != bulletPool.end(); /*nothing*/) //iterate over the deque of bullets { if((*iter)->isFired == true) //if the bullet is flying { (*iter)->move(); //move the bullet (*iter)->show(); //and show it iter++; //increment the iterator } else //otherwise the bullet is offscreen or whatever, needs removing { iter = bulletPool.erase(iter); } }}I've also tried using regular pointers, but found the same thing happened:
void unitHandler::bullets(deque<Bullet*>& bulletPool){ deque<Bullet*>::iterator iter; for(iter = bulletPool.begin(); iter != bulletPool.end(); /* */ ) { if((*iter)->isFired == false) { delete *iter; //delete the bullet object currently pointed to iter = bulletPool.erase(iter); } else { (*iter)->move(); (*iter)->show(); iter++; } } }If I take the delete line out then it doesn't freeze, but that way only the pointers to the bullets get freed, not the bullet objects, and that's bad of course.
Thanks in advance to anyone who has read this far, I'd appreciate your help. I'm hoping I'm just overlooking something obvious.
Do not mark threads 'solved' -- jpetrie
[Edited by - jpetrie on November 5, 2010 1:01:30 PM]