I'm using Visual Leak Detector and numerous calls to the crtdbg, within Visual Studio 2010, trying to find a memory leak.
As best I can tell, I have a calloc that is being called (it will hit the breakpoint that I throw on the calloc). Which means that I must have something wrong somewhere else. Sadly I cannot find this, which means that ultimately it must be a lack of understanding.
Per the following code:
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
struct Node{
char text[100];
Node *next;
Node(char* text)){
strcpy(this-text,text);
next=null;
}
}
struct List{
int NodeCount;
Node *head;
List(){
NodeCount=0;
head=NULL;
}
~List(){
this->clear();
}
void AddToList(char* text){
if (NodeCount==0)
head = new Node(text);
else {
Node *temp=head;
while (temp->next!=NULL)
temp=temp->next;
temp->next=new Node(text);
}
}
void Clear(){
Node *temp=this->head;
for (int i=0;i<NodeCount && temp!=NULL){
head=head->next;
delete temp;
temp=head;
}
if (head==NULL)
delete head;
NodeCount=0;
}
}
class Base{
public:
Base(){
//Generate array of 5 linked lists. 5 hardcoded for ease of reading.
list=(List*)calloc(5,sizeof(List)); ///THIS IS WHERE VLD SAYS MY LEAKS ARE COMING FROM///
}
~Base(){
if (list!=NULL)
//5 hardcoded for ease of reading.
for (int i=0;i<5;i++)
list[i].Clear();
free(list);
}
virtual void Update()=0;
List *list; /////LIST Member Object.
}
class Child : public Base {
public:
//actual code has paramters, hence the explicit call of the parent constructor
Child() : Base() {
//apply some basic values to the list which will always be valid
list[0].AddToList("some text from child");
}
~Child(){
//delete something extra here
}
void Update(){
//Implement pure virtual function here
}
private:
//add some extra variables here
}
void main(){
Base **BunchOfLists;
//make an array of 3 base/child objects. 3 hardcoded for ease of reading.
BunchOfLists=(Base**)calloc(3,sizeof(Base*));
BunchOfLists[0]=new Child(); //actual has parameters.
BunchOfLists[1]=new Child();
BunchOfLists[2]=new Child();
for (int i=0;i<3;i++)
delete BunchOfLists[i];
free(BunchOfLists);
}
|
Does anyone see where I could be leaking memory? I've indicated in the source where VLD indicates the problem is. In case that comment was missed, it is the Base constructor, where the List object is calloc'd. There is also a leak in the assignment of the head pointer, in AddToList, when NodeCount==0.
In my eyes, I'd have created an array of pointers to Base Objects, each pointer creating a 'new' Base object (which is eventually deleted). Each base object contains a pointer to a List object (which is eventually freed), with an allocated 5 lists which are each Clear()ed.
My theory, is that I'm not correctly adding nodes to the list... that I should be doing something like Node *temp("text"); head=&temp; within my List::AddToList?