Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #268 (closed defect: fixed)

Opened 17 years ago

Last modified 17 years ago

Memory leak in tango.io.FileSystem.getDirectory (posix part)

Reported by: xammy Assigned to: kris
Priority: minor Milestone: 0.96 Beta 2
Component: Tango Version: trunk
Keywords: malloc leak getDirectory Cc:

Description

getDirectory works this way:

char *s = tango.stdc.posix.unistd.getcwd (null, 0);
if (s)
  return new FilePath (s[0..strlen(s)]);

Because getcwd (null, 0) mallocs the returned string and those strings don't get collected by the GC, this memory is lost.

The solution, although this is a performance penalty, would be to duplicate the slice and free the original:

char *s = tango.stdc.posix.unistd.getcwd (null, 0);
if (s)
{
  char[] str = s[0 .. strlen(s)].dup;
  tango.stdc.stdlib.free(s);
  return new FilePath(str);
}

Additionally, getcwd mallocs the string only in glibc, as this is not posix compliant, see man 3 getcwd. So, maybe one could write a functions which uses the posix style and reallocates (but with GC'ed memory) the buffer, as long as it does not fit.

Proof of Concept is attached and should at least make your machine swap some space to disk ;)

Attachments

filepathtest.d (254 bytes) - added by xammy on 02/08/07 11:29:30.

Change History

02/08/07 11:29:30 changed by xammy

  • attachment filepathtest.d added.

02/11/07 00:47:42 changed by kris

  • status changed from new to closed.
  • resolution set to fixed.

thanks!