std::list<Node *> lst;
//....
Node * node = /* get from somewhere pointer on my node */;
lst.remove(node);
Does std::list::remove method call destructor(and free memory) of each removed element? If yes, how I can avoid it?
std::list<Node *> lst;
//....
Node * node = /* get from somewhere pointer on my node */;
lst.remove(node);
Does std::list::remove method call destructor(and free memory) of each removed element? If yes, how I can avoid it?
Yes, removing a
Foo*
from a container destroys theFoo*
, but it will not release theFoo
. Destroying a raw pointer is always a no-op. It cannot be any other way! Let me give you several reasons why.Storage class
Deleting a pointer only makes sense if the pointee was actually allocated dynamically, but how could the runtime possibly know whether that is the case when the pointer variable is destroyed? Pointers can also point to static and automatic variables, and deleting one of those yields undefined behavior.
Dangling pointers
There is no way to figure out whether the pointee has already been released in the past. Deleting the same pointer twice yields undefined behavior. (It becomes a dangling pointer after the first delete.)
Uninitialized pointers
It is also impossible to detect whether a pointer variable has been initialized at all. Guess what happens when you try to delete such a pointer? Once again, the answer is undefined behavior.
Dynamic arrays
The type system does not distinguish between a pointer to a single object (
Foo*
) and a pointer to the first element of an array of objects (alsoFoo*
). When a pointer variable is destroyed, the runtime cannot possibly figure out whether to release the pointee viadelete
or viadelete[]
. Releasing via the wrong form invokes undefined behavior.Summary
Since the runtime cannot do anything sensible with the pointee, destroying a pointer variable is always a no-op. Doing nothing is definitely better than causing undefined behavior due to an uninformed guess :-)
Advice
Instead of raw pointers, consider using smart pointers as the value type of your container, because they take responsibility for releasing the pointee when it is no longer needed. Depending on your need, use
std::shared_ptr<Foo>
orstd::unique_ptr<Foo>
. If your compiler does not support C++0x yet, useboost::shared_ptr<Foo>
.Never, I repeat, NEVER EVER use
std::auto_ptr<Foo>
as the value type of a container.It does call the destructor of the data in the list. That means,
std::list<T>::remove
will call the destructor ofT
(which is necessary whenT
is something likestd::vector
).In your case, it would call the destructor of
Node*
, which is a no-op. It doesn't call the destructor ofnode
.The best way to understand is to test each form and observe the results. To skillfully use the container objects with your own custom objects you need to have a good understanding of the behavior.
In short, for the type
Node*
neither the deconstructor is called nor delete/free is invoked; however, for the typeNode
the deconstructor would be invoked while consideration of delete/free is an implementation detail of list. Meaning, it depends on if the list implementation used new/malloc.In the case of a
unique_ptr<Node>
, the deconstructor is invoked and the calling of delete/free will happen since you had to give it something allocated bynew
.Pay careful attention to the memory addresses. You can tell which are pointing into the stack and which are pointing into the heap by the ranges.
Since you are putting pointers into a
std::list
, destructors are not called on the pointed-toNode
objects.If you want to store heap allocated objects in STL containers and have them be destructed upon removal, wrap them in a smart pointer like
boost::shared_ptr
Yes, though in this case, Node* has no destructor. Depending on its internals though, the various Node* values are either deleted or destroyed by scoping rules. If Node* where some non-fundamental type, a destructor would be called.
Is the destructor called on the Node? No, but 'Node' is not the element type in the list.
As to your other question, you can't. The standard list container (in fact ALL standard containers) adopt ownership of their content and will clean it up. If you don't want this to happen, the standard containers are not a good choice.
It calls the destructor of each of the items in the
list
-- but that's not aNode
object. Its aNode*
.So it doesnt delete the
Node
pointers.Does that make sense?