Description: <short summary of the patch>
 TODO: Put a short summary on the line above and replace this paragraph
 with a longer explanation of this change. Complete the meta-information
 with other relevant fields (see below for details). To make it easier, the
 information below has been extracted from the changelog. Adjust it or drop
 it.
 .
 gcl (2.6.10-6) unstable; urgency=high
 .
   * 2.6.11pre test 5
Author: Camm Maguire <camm@debian.org>

---
The information above should follow the Patch Tagging Guidelines, please
checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
are templates for supplementary fields that you might want to add:

Origin: <vendor|upstream|other>, <url of original patch>
Bug: <url in upstream bugtracker>
Bug-Debian: http://bugs.debian.org/<bugnumber>
Bug-Ubuntu: https://launchpad.net/bugs/<bugnumber>
Forwarded: <no|not-needed|url proving that it has been forwarded>
Reviewed-By: <name and email of someone who approved the patch>
Last-Update: <YYYY-MM-DD>

--- gcl-2.6.10.orig/o/alloc.c
+++ gcl-2.6.10/o/alloc.c
@@ -1635,7 +1635,7 @@ free(void *ptr) {
   if (ptr == 0)
     return;
   
-  for (p = &malloc_list,pp=*p; pp && !endp(pp);  p = &((pp)->c.c_cdr),pp=Scdr(pp))
+  for (p = &malloc_list,pp=*p; pp && !endp(pp);  p = &((pp)->c.c_cdr),pp=pp->c.c_cdr)
     if ((pp)->c.c_car->st.st_self == ptr) {
       /* SGC contblock pages: Its possible this is on an old page CM 20030827 */
 #ifdef SGC
@@ -1644,7 +1644,7 @@ free(void *ptr) {
       insert_contblock((pp)->c.c_car->st.st_self,(pp)->c.c_car->st.st_dim);
 #endif
       (pp)->c.c_car->st.st_self = NULL;
-      *p = Scdr(pp);
+      *p = pp->c.c_cdr;
 #ifdef GCL_GPROF
       if (initial_monstartup_pointer==ptr) {
 	initial_monstartup_pointer=NULL;
--- gcl-2.6.10.orig/o/gbc.c
+++ gcl-2.6.10/o/gbc.c
@@ -685,18 +685,10 @@ mark_object(object x) {
     case smm_probe:
       mark_object(x->sm.sm_object0);
       mark_object(x->sm.sm_object1);
-      if (saving_system)
-	{FILE *fp = x->sm.sm_fp;
-	if (fp != 0 /* && fp != stdin && fp !=stdout */
-	    )
-	  {fclose(fp);
-	  x->sm.sm_fp=0;
-	  }}
-      else
-	if (what_to_collect == t_contiguous &&
-	    x->sm.sm_fp &&
-	    x->sm.sm_buffer)
-	  mark_contblock(x->sm.sm_buffer, BUFSIZ);
+      if (what_to_collect == t_contiguous &&
+	  x->sm.sm_fp &&
+	  x->sm.sm_buffer)
+	mark_contblock(x->sm.sm_buffer, BUFSIZ);
       break;
       
     case smm_synonym:
@@ -1234,8 +1226,22 @@ GBC(enum type t) {
       error("GBC is not enabled");
   interrupt_enable = FALSE;
   
-  if (saving_system)
-    {t = t_relocatable; gc_time = -1;
+  if (saving_system) {
+
+    struct pageinfo *v;
+    void *x;
+    struct typemanager *tm=tm_of(t_stream);
+    unsigned j;
+
+    for (v=cell_list_head;v;v=v->next) 
+      if (tm->tm_type==v->type)
+	for (x=pagetochar(page(v)),j=tm->tm_nppage;j--;x+=tm->tm_size) {
+	  object o=x;
+	  if (type_of(o)==t_stream && !is_free(o) && o->sm.sm_fp && o->sm.sm_fp!=stdin && o->sm.sm_fp!=stdout)
+	    close_stream(o);
+	}
+
+    t = t_relocatable; gc_time = -1;
 #ifdef SGC
     if(sgc_enabled) sgc_quit();
 #endif    
--- gcl-2.6.10.orig/o/sgbc.c
+++ gcl-2.6.10/o/sgbc.c
@@ -413,18 +413,10 @@ sgc_mark_object1(object x) {
     case smm_probe:
       sgc_mark_object(x->sm.sm_object0);
       sgc_mark_object(x->sm.sm_object1);
-      if (saving_system) {
-	FILE *fp = x->sm.sm_fp;
-	if (fp != 0 /* && fp != stdin && fp !=stdout */) {
-	  fclose(fp);
-	  x->sm.sm_fp=0;
-	}
-      }
-      else
-	if (what_to_collect == t_contiguous &&
-	    x->sm.sm_fp &&
-	    x->sm.sm_buffer)
-	  mark_contblock(x->sm.sm_buffer, BUFSIZ);
+      if (what_to_collect == t_contiguous &&
+	  x->sm.sm_fp &&
+	  x->sm.sm_buffer)
+	mark_contblock(x->sm.sm_buffer, BUFSIZ);
       break;
       
     case smm_synonym:
