www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/04/28/17:35:43

Date: Fri, 28 Apr 95 15:36:59 EDT
From: John "E." Davis <davis AT space DOT mit DOT edu>
To: B DOT S DOT Runnacles AT soton DOT ac DOT uk, djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: Re: Yet more problems freeing memory

>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;
>}

I do not know that much about C++, but I do know C and you should write the
above as:

  listnode *next;
  Current = Root;
  while (Current != NULL)
    {
       next = Current->next;
       delete Current;
       Current = next;
    }

There is no guarantee that the next field of current has any meaning once
Current is deleted.  The above fragment gets it before Current is deleted.

--John

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019