tags:

views:

124

answers:

4
+4  Q: 

c++ map erase()

I have a map in which my value is dynamically allocated. When I do erase() on an element, does this free the memory OR just removes the element from the map.

I actually need to keep the memory as is. I just need to remove the element from the map since this dynamically allocated structure is used elsewhere in the code.

+6  A: 

No it doesn't free the memory if it is a naked pointer. You need to ensure that the memory is deallocated appropriately.

  • If you're using a naked pointer then make sure you clean up the memory properly when you need to.

  • If you're using a smart pointer and the map holds the last reference to the object, then the memory will be cleaned up by the smart pointer's destructor when the map erases it.

STL containers won't manage your memory, so ensure you do it. I almost always used boost's shared_ptr when putting objects into containers.

lrm
I think it's better to clearly define the ownership (whenever applicable) that blindly throw `shared_ptr` everywhere and run into the risk of cycles. Imho Boost Pointer Container are better adapted when a container owns the memory.
Matthieu M.
@Matthieu, Doing anything blindly is not recommended. Of course, using `shared_ptr` doesn't mean you're using it blindly, as you seem to assume.
lrm
Too often times the answer to "I have pointers I want to pass around" is "use shared_ptr". And that's your answer. If you look closely at the question, you'll remark that the OP **does not** want the memory to be freed when the pointer is erased from the map.... so there is no reason for him to use `shared_ptr`.
Matthieu M.
Re-read the answer again, and you'll realize you're wrong. I never said use a `shared_ptr`, I said that I often use them. Further, I said this at the end of the answer after clearly explaining what would happen with and without used a smart pointer. I'd suggest you take your misguided warpath elsewhere.
lrm
+5  A: 

When you erase from a map<something,something_else*>, it only removes the element from the map. It does not call the delete operator (or any other function) on the erased element to free memory.

Ken Bloom
+2  A: 

The standard containers will never destroy dynamically allocated objects that you place in them when you erase the elements. Basically, if you created it then you need to destroy it.

Troubadour
+2  A: 

No, the objects referred to by the pointers in your map will not be deleted.

Containers in the C++ standard library have value semantics. They will destroy the objects you put into them. When those objects are pointers, these pointers will be destroyed, but not the objects they are referring to.

Use boost_shared_ptr<> (std::map< key, boost_shared_ptr<value> >) or std::tr1::shared_ptr<> or std::shared_ptr<>, if your environment supports it, to get rid of most memory related issues.

sbi
He's not *necessarily* leaking those objects if he has another pointer to them somewhere that he's using.
Ken Bloom
@Ken: Good point, I didn't read the question well enough. Thanks for pointing it out, I fixed this.
sbi