Sign in to follow this  
Thomo

Lists and iterators

Recommended Posts

I have the following code which iterates through a list and erases an item where appropriate. The actual entry in the list is removed as planned, however the actual object is on deleted when I call: delete pParticle, even though pParticle is declared as a pointer. Anyone got any ideas??
void cParticleEmitter::Update(void)
{
	cParticle	*pParticle;

	std::list<cParticle*>::iterator pIterator = pParticleListA->begin();

	pParticle = *pIterator;

	// update the life of the particle
	pParticle->m_rLife -= g_timeElapsed / 1000.0f;

	while (pIterator != pParticleListA->end())
	{

		//destroy the particle
		if (pIterator != pParticleListA->begin() && pParticle.m_rLife <= 0)
		{
			delete (*pIterator);
			delete pParticle;
			pIterator = pParticleListA->erase(pIterator);
		}

		else
		{
			ApplyLinearForce(*pIterator);
			++pIterator;
		}
	}
}

Share this post


Link to post
Share on other sites
Your setting pParticle to the first item, then never updating it. So when you delete it later in the for loop, it's still pointing to the first one (which won't be removed from the list either). You also only ever update the time on the first particle. Is this the behavior you wanted?

All that aside, delete *pIterator; should delete what the iterator points to just fine. Set a breakpoint inside that destructor and make absolutely sure it's not being called.

Share this post


Link to post
Share on other sites
Ok, I've stepped into the code and found that the destructor for the particle IS being called with delete (*pIterator).

However I have found that the particle is trying to be rendered and has not been cleared - it is filled with garbage.

:-(

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this