Clearing memory in STL lists at shutdown

Started by
7 comments, last by izhbq412 18 years, 8 months ago
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?
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.
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();
}
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.
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]
Works like a charm now. Thanks again.
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.
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.
---------Coming soon! Microcosm II.
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.
_______________The essence of balance is detachment. To embrace a cause, to grow fond or spiteful, is to lose one''s balance after which, no action can be trusted. Our burden is not for the dependent of spirit. - Mayar, Third Keeper

This topic is closed to new replies.

Advertisement