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 (unsignedint 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?
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 (unsignedint n=0 ; n < vec.size() ; n++) {
A*it = vec.at(n);
delete(it);
it = NULL;
}
vec.clear();
}
#include <vector>
usingnamespace 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();
}
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 ;)
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.
#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();
}