Skip to main content
gamedev.net
🔒 Locked

Freeing a std::map

Started by Last Attacker 8 replies 1.2k views
Original Post
Last Attacker
Last Attacker
If I had to free an entire std::map object which contains an array of std::map's inside, can I still use the clear() function or do I have to manually go through each element in the map array and clear them first in order to avoid memory leaks? Thanks
"Take delight in the Lord and He will give you your heart's desires" - Psalm 37:4My Blog
SiCrane
SiCrane
If your map stores the maps by value, and those maps store their data types by value (instead of storing pointers to objects), then you can just use the clear() member function.

ex:
std::map > // ok to use clear
std::map * > // not ok to use clear
Last Attacker
Last Attacker
I have the following:

std::map< int, file_section_t > mFileSection;std::map< int, std::map< int, std::string > > mCacheSection;


Is that ok?
"Take delight in the Lord and He will give you your heart's desires" - Psalm 37:4My Blog
SiCrane
SiCrane
I don't know what a file_section_t is so I couldn't say about the first one. But the second one you should be able to just clear() to free (most of) the memory associated with it.
Last Attacker
Last Attacker
Quote:
I don't know what a file_section_t is so I couldn't say about the first one. But the second one you should be able to just clear() to free (most of) the memory associated with it.


Oops, sorry, its just a structure.

Thanks for helping me.
"Take delight in the Lord and He will give you your heart's desires" - Psalm 37:4My Blog
SiCrane
SiCrane
As long as that structure has a clean destructor, then calling clear() on the first one shouldn't leak memory.
Last Attacker
Last Attacker
It only has int's.

Thanks
"Take delight in the Lord and He will give you your heart's desires" - Psalm 37:4My Blog
Evil Steve
Evil Steve
To generalise: When you call std::map::clear(), the destructor is called for every key and value in the map. All STL objects will free their memory and call the destuctors for all elements inside them, including std::string and std::map.

If you have pointers (or references) in your container, they don't have destructors, so you'd have to manually delete them, either by iterating through the container and deleting what the iterator is pointing to, or if you reference the data elsewhere, free it from there.

If you use a custom class or strust in your container, then you should either not implement any constructors, destructors or copy operators and use the default (compiler-implemented) ones, or you should implement a default constructor, copy constructor, destructor, and assignment operator, so that the STL object can properly copy your type around internally.
Last Attacker
Last Attacker
Thanks for the info.

The thing is that I havn't actually used STL stuff untill today (except for vector [wink]) and I just wanted to make sure that I don't cause any unnecessary memory leakes.
I wasn't sure if it would call the destructors/constructors on creation/deletion of std::maps.
"Take delight in the Lord and He will give you your heart's desires" - Psalm 37:4My Blog
Zahlman
Zahlman
The Standard Library containers play fair: they clean up their toys, and leave you to clean up your toys (because it can't know whether you're really done with them anyway).

Incidentally, you should design your own classes this way, too (when they have "toys" to speak of, i.e. allocated memory that "belongs" to the class instance).

Topic Locked

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

Sign in to reply to this topic.