How do stl containers get deleted?

15,316

Solution 1

An STL container of pointer will NOT clean up the data pointed at. It will only clean up the space holding the pointer. If you want the vector to clean up pointer data you need to use some kind of smart pointer implementation:

{
    std::vector<SomeClass*> v1;
    v1.push_back(new SomeClass());

    std::vector<boost::shared_ptr<SomeClass> > v2;
    boost::shared_ptr<SomeClass> obj(new SomeClass);
    v2.push_back(obj);
}

When that scope ends both vectors will free their internal arrays. v1 will leak the SomeClass that was created since only the pointer to it is in the array. v2 will not leak any data.

Solution 2

If you have a vector<T*>, your code needs to delete those pointers before delete'ing the vector: otherwise, that memory is leaked.

Know that C++ doesn't do garbage collection, here is an example of why (appologies for syntax errors, it has been a while since I've written C++):

typedef vector<T*> vt;
⋮
vt *vt1 = new vt, *vt2 = new vt;
T* t = new T;
vt1.push_back(t);
vt2.push_back(t);
⋮
delete vt1;

The last line (delete vt1;) clearly should not delete the pointer it contains; after all, it's also in vt2. So it doesn't. And neither will the delete of vt2.

(If you want a vector type that deletes pointers on destroy, such a type can of course be written. Probably has been. But beware of delete'ing pointers that someone else is still holding a copy of.)

Solution 3

When a vector goes out of scope, the compiler issues a call to its destructor which in turn frees the allocated memory on the heap.

Solution 4

This is somewhat of a misnomer. A vector, as with most STL containers, consists of 2 logical parts.

  1. the vector instance
  2. the actual underlying array implementation

While configurable, #2 almost always lives on the heap. #1 however can live on either the stack or heap, it just depends on how it's allocated. For instance

void foo() { 
  vector<int> v;
  v.push_back(42);
}

In this case part #1 lives on the stack.

Now how does #2 get destroyed? When a the first part of a vector is destroyed it will destroy the second part as well. This is done by deleting the underlying array inside the destructor of the vector class.

Solution 5

Use either smart pointers inside of the vector, or use boost's ptr_vector. It will automatically free up the allocated objects inside of it. There are also maps, sets, etc.

http://www.boost.org/doc/libs/1_37_0/libs/ptr_container/doc/ptr_vector.html and the main site: http://www.boost.org/doc/libs/1_37_0/libs/ptr_container/doc/ptr_container.html

Share:
15,316
yesraaj
Author by

yesraaj

Learning c++,Check out my blog

Updated on June 04, 2022

Comments

  • yesraaj
    yesraaj almost 2 years

    How does container object like vector in stl get destroyed even though they are created in heap?

    EDIT

    If the container holds pointers then how to destroy those pointer objects

  • Martin York
    Martin York over 15 years
    This is not advisable (unless part of the destructor) as it is not exception safe. You should use a container that understands that is holding a pointer.
  • Martin York
    Martin York over 15 years
    Yes it has: boost::ptr_vector. (see boost ptr containers)
  • Dominik Grabiec
    Dominik Grabiec over 15 years
    How is it not exception safe? If ContainerType is something like std::vector<std::string*> for instance then this is perfectly valid code to put in both a destructor and a "clear" or "reset" like method.