Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Possible ztrdup memory leaks?



Peter Stephenson wrote:
> "John T. Guthrie" wrote:
> > 
> > While going through the code in exec.c, I found the following at line 2876:
> > 
> >     nam = ztrdup(nam);
> > 
> > Correct me if I'm wrong, but would this cause a memory leak?
> 
> The answer turns out to be `no' for a slightly obscure reason:  you are
> correct that the value returned by the function will not be freed, since
> it goes straight into the list of command arguments which is always on
> the heap.
> 
> However, the line in getoutputfile()
>     zaddlinknode(jobtab[thisjob].filelist, nam);
> adds the name to a list of files which will be freed when the complete
> job is finished executing.  This only happens after the shell is sure
> the command has finished with the filename added to the command
> line, so it has to use malloc memory.

I'm sorry if I'm still a little confused.  (Although I probably wasn't clear
either.)  In the line nam=ztrdup(nam), the value of nam gets overwritten by a
pointer to identical contents, but the old value of nam (that is, the value
that was used as input to ztrdup()) is now lost, and never gets freed.  I'm
still confused as to why this isn't a memory leak.  Although you did
answer another question that I had which was why the line nam=ztrdup(nam)
was necessary in the first place.

Thanks.

John Guthrie
guthrie@xxxxxxxxxxxxxxxxxx



Messages sorted by: Reverse Date, Date, Thread, Author