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

Re: Possible ztrdup memory leaks?

"John T. Guthrie" wrote:
> 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.

If you look inside gettempname(), which is where `name' comes from,
the return value comes from

  ret = ((char *) _mktemp(dyncat(unmeta(s), "XXXXXX")));

(or the other functionally identical branch of the #if).
s is a raw parameter value or static string, so doesn't need freeing.
  (getsparam() and similar function just return the string or whatever
  in the parameter without copying, for efficiency.)
`dyncat' creates a concatenated string from heap memory, so doesn't
  need freeing.  (See mem.c for a description of heap memory.)
_mktemp simply uses the space already allocated, so the return value
  doesn't need freeing.

Lots of utility functions in this neck of the words work like this.  The
unusual thing about getoutputfile() is that it acquires non-freeable
memory, but needs freeable memory for the linked list of temporary
files, hences needs to ztrdup() the string it gets.

Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070

The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
If you received this in error, please contact the sender and 
delete the material from any computer.

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