commit 33674fb58d9fc0a9d2f23eee1035df3275c5951f
parent f8d9ff5256bfc0dceb56531b1b08480a407243cd
Author: Andres Navarro <canavarro82@gmail.com>
Date: Fri, 18 Mar 2011 15:00:39 -0300
Added code to close the open files on deallocation of objects.
Diffstat:
3 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/src/Makefile b/src/Makefile
@@ -54,7 +54,7 @@ kwrite.o: kwrite.c kwrite.h kobject.h kpair.h kstring.h kstate.h kerror.h \
klisp.h kport.h
kstate.o: kstate.c kstate.h klisp.h kobject.h kmem.h kstring.h klisp.h \
kground.h kenvironment.h kpair.h keval.h koperative.h kground.h \
- krepl.h kcontinuation.h kapplicative.h kport.h ksymbol.h
+ krepl.h kcontinuation.h kapplicative.h kport.h ksymbol.h kport.h
kmem.o: kmem.c kmem.h klisp.h kerror.h klisp.h kstate.h
kerror.o: kerror.c kerror.h klisp.h kstate.h klisp.h kmem.h kstring.h
kauxlib.o: kauxlib.c kauxlib.h klisp.h kstate.h klisp.h
diff --git a/src/kport.h b/src/kport.h
@@ -20,7 +20,9 @@ TValue kmake_std_port(klisp_State *K, TValue filename, bool writep,
TValue name, TValue si, FILE *file);
/* This closes the underlying FILE * (unless it is a std port) and also
- set the closed flag to true */
+ set the closed flag to true, this shouldn't throw errors because it
+ is also called when deallocating all objs. If errors need to be thrown
+ fork this function instead of simply modifying */
void kclose_port(klisp_State *K, TValue port);
#define kport_file(p_) (tv2port(p_)->file)
diff --git a/src/kstate.c b/src/kstate.c
@@ -30,6 +30,7 @@
#include "kground.h"
#include "krepl.h"
#include "ksymbol.h"
+#include "kport.h"
/*
** State creation and destruction
@@ -464,6 +465,12 @@ void klisp_close (klisp_State *K)
klispM_free(K, (Promise *)obj);
break;
case K_TPORT:
+ /* first close the port to free the FILE structure.
+ This works even if the port was already closed,
+ it is important that this don't throw errors, because
+ the mechanism used in error handling would crash at this
+ point */
+ kclose_port(K, gc2port(obj));
klispM_free(K, (Port *)obj);
break;
default: