Skip to main content
GameDev.net gamedev.net
🔒 Locked

Lists and iterators

Started by Thomo Apr 8, 2007 at 8:01 AM 1 replies 650+ views
Original Post
Thomo
Thomo
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;
		}
	}
}

Deyja
Deyja
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.
Thomo
Thomo
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.

:-(

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.