* dynl.c (free_dynl_obj): New function to free the dynamic object
authorMarius Vollmer <mvo@zagadka.de>
Fri, 18 Jul 1997 14:31:06 +0000 (14:31 +0000)
committerMarius Vollmer <mvo@zagadka.de>
Fri, 18 Jul 1997 14:31:06 +0000 (14:31 +0000)
data. (dynl_smob): Use it.
* dynl.c (scm_dynamic_link): Moved allocating of the memory for
the dynamic object data below the linking of the object to avoid
memory leak when the linking code throws an error.  Now the code
leaks a whole dynamically linked library when must_malloc throws,
but that should be much less likely.

libguile/dynl.c

index c093af6..b486436 100644 (file)
@@ -304,6 +304,15 @@ mark_dynl_obj (ptr)
     return d->filename;
 }
 
+static scm_sizet free_dynl_obj SCM_P ((SCM ptr));
+static scm_sizet
+free_dynl_obj (ptr)
+     SCM ptr;
+{
+  scm_must_free ((char *)SCM_CDR (ptr));
+  return sizeof (struct dynl_obj);
+}
+
 static int print_dynl_obj SCM_P ((SCM exp, SCM port, scm_print_state *pstate));
 static int
 print_dynl_obj (exp, port, pstate)
@@ -322,7 +331,7 @@ print_dynl_obj (exp, port, pstate)
 
 static scm_smobfuns dynl_obj_smob = {
     mark_dynl_obj,
-    scm_free0,
+    free_dynl_obj,
     print_dynl_obj
 };
   
@@ -333,15 +342,19 @@ scm_dynamic_link (fname)
      SCM fname;
 {
     SCM z;
+    void *handle;
     struct dynl_obj *d;
 
     fname = scm_coerce_rostring (fname, s_dynamic_link, SCM_ARG1);
+
+    SCM_DEFER_INTS;
+    handle = sysdep_dynl_link (SCM_CHARS (fname), s_dynamic_link);
+
     d = (struct dynl_obj *)scm_must_malloc (sizeof (struct dynl_obj),
                                            s_dynamic_link);
     d->filename = fname;
+    d->handle = handle;
 
-    SCM_DEFER_INTS;
-    d->handle = sysdep_dynl_link (SCM_CHARS (fname), s_dynamic_link);
     SCM_NEWCELL (z);
     SCM_SETCHARS (z, d);
     SCM_SETCAR (z, scm_tc16_dynamic_obj);