From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6102 invoked from network); 20 Feb 2003 13:31:21 -0000 Received: from sunsite.dk (130.225.247.90) by ns1.primenet.com.au with SMTP; 20 Feb 2003 13:31:21 -0000 Received: (qmail 2920 invoked by alias); 20 Feb 2003 12:57:57 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 18267 Received: (qmail 2901 invoked from network); 20 Feb 2003 12:57:56 -0000 Received: from localhost (HELO sunsite.dk) (127.0.0.1) by localhost with SMTP; 20 Feb 2003 12:57:56 -0000 X-MessageWall-Score: 0 (sunsite.dk) Received: from [65.206.41.66] by sunsite.dk (MessageWall 1.0.8) with SMTP; 20 Feb 2003 12:57:56 -0000 Received: from gauss.counterexample.org (gauss.counterexample.org [192.168.13.4]) by ns00.counterexample.org (8.12.5/8.11.2) with ESMTP id h1KCvtWj016788 for ; Thu, 20 Feb 2003 07:57:55 -0500 Received: (from guthrie@localhost) by gauss.counterexample.org (8.11.6/8.11.6) id h1KCvsU22058 for zsh-workers@sunsite.dk; Thu, 20 Feb 2003 07:57:54 -0500 From: "John T. Guthrie" Message-Id: <200302201257.h1KCvsU22058@gauss.counterexample.org> Subject: Re: Possible ztrdup memory leaks? To: zsh-workers@sunsite.dk Date: Thu, 20 Feb 2003 07:57:54 -0500 (EST) In-Reply-To: <3175.1045737902@csr.com> from "Peter Stephenson" at Feb 20, 2003 10:45:02 AM X-Mailer: ELM [version 2.5 PL6] MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit 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@counterexample.org