[gfortran] Memory leak with internal files.

Paul Brook paul@codesourcery.com
Thu Oct 7 21:33:00 GMT 2004


The patch below fixes a memory leak with internal files. It closes the stream 
object when an transfer statement on an internal unit completes. It also 
makes teh mem_close routien free the stream object.

Without this patch the following program would explode.

program prog
  character(len=10) :: s
  integer i
  integer j

  j = 0
  s = "42"
10 continue
  read (unit=s,fmt='(I4)') i
  j = j + 1;
  if (mod(j, 1000) .eq. 0) print *, j
  goto 10
end program

Tested on i686-linux.
Applied to mainline.

Paul

2004-10-07  Paul Brook  <paul@codesourcery.com>

 * io/unix.c (mem_close): Free stream object.

Index: io/transfer.c
===================================================================
RCS file: /var/cvsroot/gcc-cvs/gcc/libgfortran/io/transfer.c,v
retrieving revision 1.16
diff -u -p -r1.16 transfer.c
--- io/transfer.c 6 Oct 2004 18:27:58 -0000 1.16
+++ io/transfer.c 7 Oct 2004 21:17:53 -0000
@@ -1349,7 +1349,8 @@ next_record (int done)
 
 
 /* Finalize the current data transfer.  For a nonadvancing transfer,
-   this means advancing to the next record.  */
+   this means advancing to the next record.  For internal units close the
+   steam associated with the unit.  */
 
 static void
 finalize_transfer (void)
@@ -1392,6 +1393,9 @@ finalize_transfer (void)
     }
 
   sfree (current_unit->s);
+
+  if (is_internal_unit ())
+    sclose (current_unit->s);
 }
 
 
Index: io/unix.c
===================================================================
RCS file: /var/cvsroot/gcc-cvs/gcc/libgfortran/io/unix.c,v
retrieving revision 1.10
diff -u -p -r1.10 unix.c
--- io/unix.c 18 Sep 2004 23:55:22 -0000 1.10
+++ io/unix.c 7 Oct 2004 20:52:27 -0000
@@ -811,6 +811,7 @@ mem_truncate (unix_stream * s)
 static try
 mem_close (unix_stream * s)
 {
+  free_mem (s);
 
   return SUCCESS;
 }



More information about the Gcc-patches mailing list