Deleting inactive things from my list... I think I'm missing something

Recommended Posts

Hi all, this is part of my game, but I think it's a general programming oversight on my part. I'm using VC++ 2008 express edition on Windows XP Pro SP3. Anyway, the situation is this: I have a list with the various objects that get updated in the game. Basically, I have these lines when I do anything with it:
//In the main game class, it's declared like this:
std::list< smartptr<GameObject> > GameObjs;

//This declares it...
std::list< smartptr< GameObject > >::iterator it1;

//During the run, this is called...
it1 = GameObjs.begin();
while(it1 != GameObjs.end())
{//	(*it1)->Update();  THIS LINE is updating without deletion...  Obviously the wrong way to go when many objects are in play.
if((*it1)->Update())
it1 = GameObjs.erase(it1);
else
++it1;
}

//Later, while drawing:
for (int blah = 1; blah < 7; blah++)
{	for(it1 = GameObjs.begin(); it1 != GameObjs.end(); ++it1)
{	if ((*it1)->drawPriority == blah)
(*it1)->Draw();
}
}

http://pastebin.com/rGswatvz - This is the smartPtr source. Basically, what SHOULD be happening is, during the Update call, if true is returned, the object should just be deleted from the list. This does not seem to be the case though as I can have player bullets off-screen "hit" enemies. Another thing that's happening is the App crashes when closing. This tends to only happen if I shoot a fair amount of bullets, but obviously it should not be happening period. I've commented out the removal code and the App runs okay until a LOT of bullets/enemies are in there and never being removed (obviously). But the thing never crashes upon closing, so I'm positive this is the culprit. Anyone have any thoughts? Thanks. [Edited by - Sivak on March 29, 2010 2:18:52 PM]

Share on other sites
Why are you calling Update() twice?

Share on other sites
Quote:
 Original post by Gage64Why are you calling Update() twice?

Er, sorry. The part below the initial call is supposed to be commented out. I've fixed this.

Share on other sites
Hey all. Sorry for the double post, but I seem to have figured it out. I needed a virtual dtor in the master class. That seems to have fixed it!

Create an account

Register a new account

• Partner Spotlight

• Forum Statistics

• Total Topics
627681
• Total Posts
2978611

• 13
• 12
• 10
• 12
• 22