delete pointers from vector

i have something like this:

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
class A {
};

class B : public A {
};

class C : public A {
};

B*b1;
B*b2;
C*c1;
C*c2;

vector<A*>vec;

int main() {
   vec.push_back(b1);
   vec.push_back(b2);
   vec.push_back(c1);
   vec.push_back(c2);

   for (unsigned int n=0 ; n < vec.size() ; n++) {
      A*it = vec.at(n);
      delete(it);
      it = NULL;
   }
   vec.clear();
}


and it don't works at all. all i get (when playing with variations of this stuff until it compiles correctlly) is a memory leak.

for example, let say i have b1 adress = 1234
i will effectivelly free the memory at 1234, but for a strange reason, the memory leak is elsewhere, for example, at 2345, and the memory value at this adress is equal to... 1234, the adress of the pointer i wanted to delete.

does anybody have any experience with this kind of low level memory management?

thanks


Last edited on
Well you're attempting to delete objects on the stack. You only delete memory you've allocated on the heap.
well, i omited to precise, before to delete objects, i create new objects:
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
class A {
};

class B : public A {
};

class C : public A {
};

B*b1;
B*b2;
C*c1;
C*c2;

vector<A*>vec;

int main() {
   b1 = new B(); //create new objects
   b2 = new B(); //
   c1 = new C(); //
   c2 = new C(); //here
   vec.push_back(b1);
   vec.push_back(b2);
   vec.push_back(c1);
   vec.push_back(c2);

   for (unsigned int n=0 ; n < vec.size() ; n++) {
      A*it = vec.at(n);
      delete(it);
      it = NULL;
   }
   vec.clear();
}

sorry for the mistake
Use an iterator:

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
34
35
#include <vector>

using namespace std;

class A {
};

class B : public A {
};

class C : public A {
};

B*b1;
B*b2;
C*c1;
C*c2;

vector<A*>vec;

int main() {
   vec.push_back(new B());
   vec.push_back(new B());
   vec.push_back(new C());
   vec.push_back(new C());

   for (vector<A*>::iterator it = vec.begin(); it != vec.end(); ++it)
   {
      A* a = *it;
      delete a;
      a = NULL;
   }

   vec.clear();
}
Last edited on
thanks, it seems to resolve partlly the problem. but in my specific implementation, i get a fixed 44 bytes leak, even when i run the allocation/delete part in a 10000 times loop, the same if i execute only one time this loop.

the first leak is a 36 bytes one, with the content equal to a list of pointers to deleted objects.
the second is still not identified.

i presume it's due to the c++ run time architecture (or something alike) that will create an array of pointers in the scope when i call the new directive, and this array is not cleaned, even if i close the scope.

if i delete items one by one using the explicit label it goes well.

it is a little annoying to don't have the freedom to handle memory like we want... but i'm close to solution, i feel it ;)
Last edited on
Well you no longer need:

1
2
3
4
B*b1;
B*b2;
C*c1;
C*c2;


I should have deleted those from the example code.

How are you identifying a memory leak? Does this make a difference:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
   {
      vec.push_back(new B());
      vec.push_back(new B());
      vec.push_back(new C());
      vec.push_back(new C());

      for (vector<A*>::iterator it = vec.begin(); it != vec.end(); ++it)
      {
         A* a = *it;
         delete a;
         a = NULL;
      }

      vec.clear();
   }
}
i identify the leaks with the microsoft tool _CrtDumpMemoryLeaks(); under vc++ 2010.

i am currentlly trying to test under linux with valgrind to see the exact problem.

i suppose the manually allocated pointers are the source of the problem cause it correspond exactlly to the number of pointers i allocated (9 * 4 = 36) plus one pointer to a 8 bytes zone for the pointer to the manager...
maybe the leak does not exist anymore...

i'll tell you when i get the valgrind result, if i get one.

thanks.
For what it's worth... if you use smart pointers, you'll never have this problem.

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
34
35
36
#include <vector>
#include <memory>

class A
{
public:
    virtual ~A() { }  // if you're deleting polymorphically... you MUST give it a virtual dtor
        // do it do it do it
};

class B : public A { };
class C : public A { };


typedef std::unique_ptr<A> APtr;  // or shared_ptr if you prefer
std::vector< APtr > vec;

int main() {
    vec.push_back( std::move( APtr( new B() ) );
    vec.push_back( std::move( APtr( new B() ) );
    vec.push_back( std::move( APtr( new C() ) );
    vec.push_back( std::move( APtr( new C() ) );

/*
   // don't need this crap anymore
   for (vector<A*>::iterator it = vec.begin(); it != vec.end(); ++it)
   {
      A* a = *it;
      delete a;
      a = NULL;
   }
*/

   // this will automatically delete all removed elements.
   vec.clear();
}
Topic archived. No new replies allowed.