Jump to content
  • Advertisement
Sign in to follow this  
CIJolly

Clearing memory in STL lists at shutdown

This topic is 4841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having a few problems implimenting the STL in my game. When a press space I create a bullet with my_bullet = new CBullet(x, y); Then I store it in a container that I've declared earlier with bullet_list.push_back(*my_bullet); I loop through the list to deal with each bullet moving with for (bullet_iter = bullet_list.begin(); bullet_iter != bullet_list.end(); bullet_iter++) { bullet_iter->MoveBullet; } When I close the program, however, I get an error message "This program encountered a problem and needs to shut down". I assume that this is because I haven't properly cleaned up the data on shutdown. I've tried bullet_list.clear(); but that just seems to clear the references to the data from the list. Looping through the data the same way I did for moving the bullets and trying to delete *bullet_iter doesn't work either. Does anyone know a good way to clear the memory allocated to instances stored in an STL list?

Share this post


Link to post
Share on other sites
Advertisement
Make the list a list of pointers and delete the freed memory after you're done doing what the app does. I've never had that problem when I get a memory leak though, so it might not be because of this. You don't have to clear lists on shutdown though, but it would be a very good idea to clean up the memory you allocated.

Share this post


Link to post
Share on other sites
It's because your list contains copies of the newly allocated objects and not the allocated object addresses (therefore "losing" the new allocation), your container should refer to pointers instead.

To cleanup use:

while( bullet_list.size() )
{
delete *(bullet_list.back());
bullet_list.pop_back();
}

Share this post


Link to post
Share on other sites
post the list declaration

this code:
bullet_list.push_back(*my_bullet);

implies you aren't storing pointers, but actual CBullets, in which case you don't need to call delete yourself, however you are still leaking memory for every new, you are just pushing a copy of CBullet onto the list.

Share this post


Link to post
Share on other sites
The list declaration:

list<CBullet>::iterator bullet_iter;
list<CBullet> bullet_list;

I probably am just storing object. That would explain a program crashing error I got after using push_back() to store the object, then deleting the temporary object to free it's memory.
The program would have been trying to move a bullet that didn't exist any more.

I will try and rewrite it using a list of pointers.

Thanks for the help everyone.

[Edited by - CIJolly on August 11, 2005 11:17:17 PM]

Share this post


Link to post
Share on other sites
Instead of

my_bullet = new CBullet(x, y);
bullet_list.push_back(*my_bullet);

why dont you just do:

CBullet my_bullet(x,y);
bullet_list.push_back(my_bullet);

(with std::list<CBuffer> bullet_list;)
That way you dont have to worry about freeing up memory on the heap. my_bullet will be a local variable on the stack and stl::list will make a copy of it to store. If the default copy constructor isn't going to work then you will need to specify one explicitly for this to work.

If you don't use a list of pointers then you dont have to worry about allocation and deallocation at all.

Share this post


Link to post
Share on other sites
If you don't use pointers, you have to worry that the push_back call will copy the object correctly. and in my past experience, it doesnt.

Share this post


Link to post
Share on other sites
Quote:
Original post by xSKOTTIEx
If you don't use pointers, you have to worry that the push_back call will copy the object correctly. and in my past experience, it doesnt.

In your past experience did you define a copy constructor? :)

If you want to avoid storing the objects (for performance reasons or because you need to store polymorphic classes) you can store reference-counting smart pointers to your objects that will do the cleanup automatically when they are no longer referenced.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!