From cee674c3da09b1a8ff796762329d0a48f723caa3 Mon Sep 17 00:00:00 2001 From: simonmar Date: Tue, 28 Mar 2000 08:49:56 +0000 Subject: [PATCH] [project @ 2000-03-28 08:49:56 by simonmar] We weren't releasing the memory associated with dead file objects (including the possibly large buffer). This commit fixes that. --- ghc/lib/std/cbits/closeFile.c | 12 +++++++++++- ghc/lib/std/cbits/freeFile.c | 13 +++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/ghc/lib/std/cbits/closeFile.c b/ghc/lib/std/cbits/closeFile.c index f144c41..3c3eb4a 100644 --- a/ghc/lib/std/cbits/closeFile.c +++ b/ghc/lib/std/cbits/closeFile.c @@ -1,7 +1,7 @@ /* * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998 * - * $Id: closeFile.c,v 1.8 2000/03/14 01:52:25 sof Exp $ + * $Id: closeFile.c,v 1.9 2000/03/28 08:49:56 simonmar Exp $ * * hClose Runtime Support */ @@ -60,6 +60,14 @@ closeFile(StgForeignPtr ptr, StgInt flush_buf) } + /* Free the buffer straight away. We can't free the file object + * itself until the finalizer runs. + */ + if ( fo->buf != NULL ) { + free(fo->buf); + fo->buf = NULL; + } + /* Closing file descriptors that refer to standard channels is problematic, so we back off from doing this by default, just closing them at the Handle level. If you insist on @@ -89,6 +97,8 @@ closeFile(StgForeignPtr ptr, StgInt flush_buf) } } } + fo->fd = -1; + return 0; } diff --git a/ghc/lib/std/cbits/freeFile.c b/ghc/lib/std/cbits/freeFile.c index ffe5723..aecf498 100644 --- a/ghc/lib/std/cbits/freeFile.c +++ b/ghc/lib/std/cbits/freeFile.c @@ -1,7 +1,7 @@ /* * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998 * - * $Id: freeFile.c,v 1.8 1999/11/26 16:25:56 simonmar Exp $ + * $Id: freeFile.c,v 1.9 2000/03/28 08:49:56 simonmar Exp $ * * Giving up files */ @@ -65,7 +65,11 @@ freeFileObject(StgForeignPtr ptr) * (via closeFile()), we will have given * up our process lock, so we break off and just return. */ - return; + if ( fo->buf != NULL ) { + free(fo->buf); + } + free(fo); + return; } if (fo->buf != NULL && fo->bufWPtr > 0) { @@ -84,6 +88,11 @@ freeFileObject(StgForeignPtr ptr) #endif /* Error or no error, we don't care.. */ + if ( fo->buf != NULL ) { + free(fo->buf); + } + free(fo); + return; } -- 1.7.10.4