Memory leak using STL List

Started by
16 comments, last by the_edd 16 years, 7 months ago
Quote:Original post by matth
Projectile *projSwap;projSwap = new Projectile();//this is a memory leakprojSwap = (*projNode);Enemy *enemySwap;enemySwap = new Enemy(); //this is a memory leakenemySwap = (*enemyNode);}


You are allocating memory for a projectile object, then immediately assigning the pointer to a different block of memory. Don't do this.
Advertisement
The following should work:

// Check for Collisionsfor( std::list<Projectile*>::iterator projNode = ProjectileList.begin(); projNode != ProjectileList.end(); ++projNode ){	Projectile *projSwap = (*projNode);	if(SpriteCollisionTest(dynamic_cast<Sprite&>(*projSwap), dynamic_cast<Sprite&>(*playerMothra)))	{		(*projNode)->Kill();	}	else	{				for( std::list<Enemy*>::iterator enemyNode = EnemyList.begin(); enemyNode != EnemyList.end(); ++enemyNode )		{			Enemy *enemySwap = (*enemyNode);						if(SpriteCollisionTest(dynamic_cast<Sprite&>(*projSwap), dynamic_cast<Sprite&>(*enemySwap)))			{				(*projNode)->Kill();				(*enemyNode)->Hit();			}                        //delete enemySwap; <---remove		}	}	//delete projSwap; <---remove}


Keep in mind smart pointers are NOT STL container safe. I normally store
plain old pointers in the container and when I need to move it around I throw
it into a smart pointer.
Check out the OpenMMOG Engine at www.openmmog.com
Quote:Original post by Moss
Keep in mind smart pointers are NOT STL container safe. I normally store
plain old pointers in the container and when I need to move it around I throw
it into a smart pointer.


std::auto_ptr isn't, boost::shared_ptr is.
Quote:Original post by Moss
The following should work:

*** Source Snippet Removed ***


Not really. You are deleting every item in the list, but still leaving them in there. I get the feeling that you shouldn't be deleting any of them, but if you do, remove them from the list so you don't access deleted memory later.
Yes your correct the 2 deletes should be removed also.
Check out the OpenMMOG Engine at www.openmmog.com
Thanks for all the help guys - as usual I am able to find my errors a whole lot easier once I start asking questions.

I changed the following line :

if(SpriteCollisionTest(dynamic_cast<Sprite&>(*projSwap), dynamic_cast<Sprite&>(*enemySwap)))

over to:

if(SpriteCollisionTest(dynamic_cast<Sprite&>(**projNode), dynamic_cast<Sprite&>(**enemyNode)))

And took out all the unneeded enemySwap/projSwap stuff
Quote:Original post by matth
Thanks for all the help guys - as usual I am able to find my errors a whole lot easier once I start asking questions.

I changed the following line :

if(SpriteCollisionTest(dynamic_cast<Sprite&>(*projSwap), dynamic_cast<Sprite&>(*enemySwap)))

over to:

if(SpriteCollisionTest(dynamic_cast<Sprite&>(**projNode), dynamic_cast<Sprite&>(**enemyNode)))

And took out all the unneeded enemySwap/projSwap stuff


Did you miss the last bit of my post?

Quote:Source: me [smile]
Quote:if(SpriteCollisionTest(dynamic_cast<Sprite&>(*projSwap), dynamic_cast<Sprite&>(*playerMothra)))

Will not work as you intend, as a bad dynamic_cast on a reference will throw an exception (it cannot return a "null" reference because no such thing exists). It is one of the few times it is preferable to use a pointer over a reference.
Is there any reason that you have to do all these dynamic allocations, anyway?

Why not a list<Projectile> instead of a list<Projectile *>?

It's probably just as efficient for small objects (which I assume Projectile is) and you won't have to worry about when to delete, it it's null-valued, what happens in the case of exceptions and adding deletes in the case of early returns and etc.

Edd

This topic is closed to new replies.

Advertisement