Will this give me memory leaks regarding to std::vector clear and erase function

As I read in the reference page for std::vector clear function()

" All the elements of the vector are dropped: their destructors are called, and then they are removed from the vector container, leaving the container with a size of 0."

It saids that it would call all the of destructors of the erasing elements in side that vector when clear() is called. Just wondering if this is true? Because I went through with the GDB debugger it doesn't seem to call the destructors for them.


Here a test code that I wrote just wondering will this give me memory leak if delete is not called.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

void some_function()
{
   int *int1 = new int(1);
   int *int2 = new int(2);
   int *int3 = new int(3);

   std::vector<int*> vec;

   vec.push_back(int1);
   vec.push_back(int2);
   vec.push_back(int3);

   vec.clear();
   
   printf("%d\n",*int1);//this prints 1
   
   delete int1;

   printf("%d\n",*int1);//this prints garbage

}


if above is the case then what described in the description about clear is not true or am I missing something.


Last edited on
Yes, that's a memory leak.
The vector does call the destructor of its elements. But in your example the elements are pointers, not objects.

The destructor of a pointer does not delete it.
Think about why this is the expected behaviour.
thanks for the reply

so what is the proper way for the clear function to call its destructor
Use std::auto_ptr <T> or for the new C++0x standard use std::unique_ptr <T> .
Last edited on
Again, it does call the destructors.
In your example, you should simply std::vector<int> vect;

If you need to use pointers because you want a polymorphic container, I know two possibilities:
_ Use smart_pointers. As STL containers use copy mechanism (c++98) you will need shared_ptr
_ Especialize a container to handle pointers. By instance boost::ptr_vector. I guess that you could simply modify the allocator used, making the destruct() method responsible for the delete, but I haven't tested that.
so I guess why my way of copying vectors that are holding pointer elements also not working, even though I overload the = for that pointer element.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

void some_function()
{
    std::vector<int*> vec1;  
    
   
    
    std::vector<int*> vec2;


   //assume that vec1 is holding a few int* elements and vec2 is empty.

   //when I do
    vec2 = vec1;

 //  vec2 now only contain pointers of int not the actualy object.
    

    // manually erase all objects in vector instead of calling vec1.clear()
    for(std::vector<int*>::iterator iter = vec1.begin();iter != vec.end();iter++)
   {
      int *temp = *iter;
   
      delete temp;
     temp = NULL;
   }

   now vec2.at(0) would contain garbage
  
   
}



So I guess my question is what is a proper to copy std::vector<int*> thanks. or I have to manually do it like what I did with delete. thank-you
If you want to copy the objects pointed, use copy_ptr
even though I overload the = for that pointer element.
AFAIK you can't do that
Hi thanks replying to my posts

Would you so kind and give me a good example link on how to incoporate shared_ptr and copy_ptr with vector std thanks.

Topic archived. No new replies allowed.