Re: [cc65] Need some help with a memory leak

From: Payton Byrd <plbyrd1gmail.com>
Date: 2010-05-04 22:32:00
On Tue, May 4, 2010 at 3:22 PM, Marc 'BlackJack Rintsch <marc@rintsch.de>wrote:

> On Tuesday 04 May 2010, Payton Byrd wrote:
> > I'm stumped trying to track down a memory leak and I'm hoping someone
> > here can spend a little time to help me out.  The only place in my
> > app where I'm dynamically allocating memory is creating the linked
> > list that holds the directory information for a drive, so I know it's
> > gotta be in there somewhere.  However, I believe I've been very
> > thorough in my cleanup code, but I could be wrong.
> >
> > All of the code for this bug can be found at these URLs:
> >
> > drives.h -
> > http://cbmcommand.codeplex.com/SourceControl/changeset/view/43431#834
> >475
> > <http://cbmcommand.codeplex.com/SourceControl/changeset/view/43431#83
> >4475>drives.c -
> > http://cbmcommand.codeplex.com/SourceControl/changeset/view/43431#834
> >476
> >
> > <http://cbmcommand.codeplex.com/SourceControl/changeset/view/43431#83
> >4476>The problem appears in the getDirectory method.  This is the only
> > place where memory is dynamically allocated and freed.  Help is
> > GREATLY appreciated.
>
> IMHO it's quite obvious in that first ``while`` construct.  Just grab
> pencil and paper and "step through" the code with two entries.
>
> 1. Current is the second one.  The loop body will be entered but the
> `next` pointer is null so the current pointer will be set to the first
> entry.

2. Now the loop body won't get executed because of the second condition:
> `prev` of the first entry is null.
> 3. The first entry will be freed after the loop.
>
> The second entry is still allocated at this point.
>
>
First off, THANK YOU for taking a look at this.  I appreciate it greatly.
 Maybe I'm wrong, but here's how I interpret the code.

currentNode = drive->tail;
 while(currentNode != NULL && currentNode->prev != NULL)
{
 if(currentNode->next != NULL)
free(currentNode->next);

 currentNode = currentNode->prev;
}
if(currentNode != NULL)
 free(currentNode);

First step sets currentNode to the tail of the list.
Next, we loop until currentNode is NULL or currentNode->prev is NULL.  In a
directory of any files this will be false at least once.
Next, we look at currentNode->next and if it's not null we free it.
Next, we move to one node to the left.
Once we complete the loop, currentNode should have both prev = NULL, and the
pointer for next should be freed, leaving us free to free currentNode.



> Ciao,
>        Marc 'BlackJack' Rintsch
> --
> “Programmers should be paid by the amount of code
>  that they avoid writing.”  -- Michael P. Soulier
>



-- 
Payton Byrd
<http://www.paytonbyrd.com>
<http://it.toolbox.com/blogs/paytonbyrd>

----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Tue May 4 22:32:28 2010

This archive was generated by hypermail 2.1.8 : 2010-05-04 22:32:30 CEST