views:

145

answers:

5

Title pretty much covers it. If I've added say 3 objects to a list and the list goes out of scope and dies, will it call delete on each entry before going out of scope? Pretty sure yes, but getting tired and need a sanity check.

+5  A: 

Survey says: I'm tired and no they won't delete themeselves unless they are within some sort of container safe smart pointer in the list itself. Nap time...

Michael Dorgan
+2  A: 

No, no STL container will ever delete your pointers. This because STL container never take ownership of your pointers, and so STL containers never take the blame for double deletes.

wilhelmtell
+3  A: 

If you've got a std::list<T> my_list; and then my_list goes out of scope, each T object within the list will be deleted.

The question now is, what objects are you storing in your container?

If you have real objects in your container, e.g. std::list<MyClass>, the objects will be deleted and MyClass::~MyClass will be called for each one of them.

If instead you only have pointers to objects, e.g. std::list<MyClass*>, the pointers will be deleted, but the pointed-to objects will not be deleted!

If you need to store pointers to objects and want the pointed-to objects to be destroyed when the container dies, you'll have to use some some of smart pointers (e.g. boost::smart_ptr*), which delete their object as the pointers themselves get deleted. Or you'll need to manually go through the list and destroy objects before you destroy the container of pointers, but this is more error-prone and may need extra work to make sure you do that in case of an exception.

*std::auto_ptr has an unusual operator=() which is incompatible with the requirements of containers. (Thanks j-random-hacker -- please give him +1 below...)

squelart
+1 but please don't recommend `auto_ptr` for STL containers -- `auto_ptr` has an unusual `operator=()` which is incompatible with the requirements of containers. `boost::smart_ptr` is suitable though.
j_random_hacker
In particular, `auto_ptr` objects don't fulfill the STL containers requirement that after the statement `a=b` objects `a` and `b` are equivalent. This is because the assignment operator of `auto_ptr` doesn't "assign" in the usual sense but rather _transfers ownership_ of the underlying pointer from object `b` to object `a`. The objects thus aren't equivalent after the assignment: one owns the pointer, the other doesn't. The reason they behave this way is because this is their approach to prevent double-deletes: effective, yet at times too simple.
wilhelmtell
+3  A: 

No, the objects will not be deleted.

Boost has a solution, the Boost Pointer Container Library. Not only does it delete the pointers for you, but it enhances the syntax of common operations to make them more convenient.

Mark Ransom
A: 

No. 'delete' will not be called on each item. If you want that to happen, then the list should hold smart pointers instead of plain pointers. Refer C++ STL vector of pointers. If you really hate to have a memory leak in your application, you need to call delete explicitly on each pointer before the list goes out of scope.

Prabhu
Your explanation is good, but squelart said much the same thing 2 hours earlier. Gotta be fast in this game! :)
j_random_hacker