Reputation: 1356
I enabled heap debugging because of a memory leak error I started seeing in somebody else's code and I pinned down the problem (at least, so I think) to the destructor of a class, right after the call to the delete []
below.
MyClass::~MyClass() {
delete [] my_class_member_;
}
Now I confirmed that my_class_member_ which is an array of pointers to objects of a struct, say, MyStruct
, has been allocated property using new []
so I am not sure what is causing this leak? Here's what the new []
call looks like:
delete [] my_class_member_;
my_class_member_ = new MyStruct[somesize_];
Next, the struct MyStruct
is also relatively simple and is as follows (stripped down):
struct MyStruct {
MyStruct() {}
~MyStruct() {
for( PtrList<PClass>::iterator it(ps); it.more(); it.next() ) {
delete it.cur();
}
for( PtrList<RClass>::iterator it(rs); it.more(); it.next() ) {
delete it.cur();
}
delete shift;
}
PtrList<PClass> ps;
PtrList<RClass> rs;
};
Now PtrList
is a list of pointers (a built-in type of the application devkit we're using).. I am pretty certain that can't be at fault. Anyways, is anything out of whack that anybody notices here?? Appreciate any insight..
Upvotes: 0
Views: 106
Reputation: 206526
Adding comment as an answer as requested by OP.
Have you followed the Rule of Three?
From the information you present it is not clear whether you do so.You need to provide a copy constructor and copy assignment operator which do a deep copy of your dynamically allocated member.If you don't do so and if both these operators are not private
and not left undefined then You cannot be sure that they are not being used at all and this is where your problem lies
Upvotes: 1