[MLton-devel] Memory Leak in StreamIO functor

Jared M Showalter jareds@mit.edu
Mon, 13 Oct 2003 00:16:51 -0400 (EDT)


Hi,

This is a bug report with a short patch against MLton-20030716 (I imagine
it will work against later versions unless streams are drastically
changed).

A program that I wrote was using substantially more memory than I
expected.  I was able to isolate the problem to the fact that functional
instreams use space proportional to the amount of input they've read,
rather than constant space.  (I was using them in a properly tail
recursive manner.)

Poking in the basis library source, I found that the StreamIO functor was
keeping track of open instreams to close on exit by placing newly created
instreams in a list.  This, naturally, is bad.  I made a simple fix that
clears the buf field before placing an instream in the list.  It follows.

Jared

diff -u mlton-20030716/basis-library/io/stream-io.fun mlton-new/basis-library/io/stream-io.fun
--- mlton-20030716/basis-library/io/stream-io.fun	2003-05-09 08:48:34.000000000 -0400
+++ mlton-new/basis-library/io/stream-io.fun	2003-10-12 23:37:39.000000000 -0400
@@ -318,6 +318,7 @@
 
       val empty = V.tabulate (0, fn _ => someElem)
       val line = V.tabulate (1, fn _ => lineElem)
+      fun stripInstream is = updateBufBeg (is, Buf {inp = empty, base = NONE, next = ref End})
 
       fun extend function
 	         (is as In {common = {augmented_reader, tail, ...}, ...})
@@ -802,7 +803,7 @@
 				  buffer_contents = buffer_contents}
 	    val _ = if closed
 		      then ()
-		      else openInstreams := (is,atExit) :: (!openInstreams)
+		      else openInstreams := (stripInstream is,atExit) :: (!openInstreams)
 	  in
 	    is
 	  end
diff -u mlton-20030716/basis-library/io/stream-io.sig mlton-new/basis-library/io/stream-io.sig
--- mlton-20030716/basis-library/io/stream-io.sig	2003-05-02 19:49:46.000000000 -0400
+++ mlton-new/basis-library/io/stream-io.sig	2003-10-12 23:35:48.000000000 -0400
@@ -42,6 +42,7 @@
       val mkInstream': {reader: reader,
 			closed: bool,
 			buffer_contents: vector option} -> instream
+      val stripInstream: instream -> instream
 
       val equalsOut: outstream * outstream -> bool
       val outstreamWriter: outstream -> writer


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
SourceForge.net hosts over 70,000 Open Source Projects.
See the people who have HELPED US provide better services:
Click here: http://sourceforge.net/supporters.php
_______________________________________________
MLton-devel mailing list
MLton-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mlton-devel