Mail Archives: djgpp/1995/04/28/17:47:44
Date: Fri, 28 Apr 1995 15:52:51 +0100 (BST)
From: "B.S.Runnacles" <B DOT S DOT Runnacles AT soton DOT ac DOT uk>
Dear all..
I wrote recently of a problem I was having freeing up the memory associated
with a linked list. It transpires that go32 does not actually report
the memory being returned to the heap. My problem is that my code is leaking
memory somewhere, in that it progressively eats up more and more memory in
the large number of iterations required, eventually running out of swap space.
I think I've traced the leak to the linked list objects I use, can anyone spot
what is wrong here:
private:
listnode *Current,*Root
LinkedList::~LinkedList(void)
{
for(Current = Root; Current!=NULL; Current=Current->next)
delete Current;
}
OUCH! When the assignment happens at the end of the loop:
(Current=Current->next) Current has already been freed and **MUST** not be
referenced for the assignment! You may be trashing malloc's data structures
eventually or some such. Try the following:
LinkedList::~LinkedList(void)
{
listnode *tmp;
for(Current = Root; Current!=NULL; Current=tmp) {
tmp = Current->next;
delete Current;
}
}
BTW: Like everyone else I have a Linked List class you're welcome to if you
want it.
--
Art S. Kagel, kagel AT ts1 DOT bloomberg DOT com
- Raw text -