changeset 579:81053b8bdb51

Yet fewer memory leaks in the PD external Close opened databases before mutating the x->db pointer in create and open methods.
author mas01cr
date Mon, 06 Jul 2009 17:13:49 +0000
parents dabbde72e331
children 633614461994
files bindings/pd/adbpd.c
diffstat 1 files changed, 19 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/bindings/pd/adbpd.c	Mon Jul 06 16:44:59 2009 +0000
+++ b/bindings/pd/adbpd.c	Mon Jul 06 17:13:49 2009 +0000
@@ -45,6 +45,7 @@
 
 static void adbpd_create(t_adbpd *, t_floatarg, t_floatarg, t_floatarg);
 static void adbpd_open(t_adbpd *x);
+static void adbpd_close(t_adbpd *x);
 static void adbpd_setname(t_adbpd *x, t_symbol *s, int argc, t_atom *argv);
 static void adbpd_status(t_adbpd *x);
 static void adbpd_l2norm(t_adbpd *x);
@@ -96,10 +97,7 @@
 }
 
 static void adbpd_free(t_adbpd *x) {
-  if(x->db) {
-    audiodb_close(x->db);
-    x->db = NULL;
-  }
+  adbpd_close(x);
 }
 
 /* bang executes the defined query */
@@ -109,29 +107,38 @@
 
 /* create a database */
 static void adbpd_create(t_adbpd *x, t_floatarg datasize, t_floatarg ntracks, t_floatarg dim) {
-  post("creating db '%s'", x->x_dbname->s_name);
+  adbpd_close(x);
+
+  post("Creating db '%s'", x->x_dbname->s_name);
   x->db = audiodb_create(x->x_dbname->s_name, datasize, ntracks, dim);
 
   if (x->db) {
     post("Created and opened");
   } else {
-    error("Could not create db '%s'.");
+    error("Could not create db '%s'.", x->x_dbname->s_name);
   }
 }
 
-/* open a database. Need to work out the path stuff here. At present
-   databases need to be in the root directory. Not good. */
+/* open a database, closing an existing one if necessary. */
 static void adbpd_open(t_adbpd *x) {
+  adbpd_close(x);
+
   post("Opening db '%s'", x->x_dbname->s_name);
-  x->db = audiodb_open(x->x_dbname->s_name, O_RDWR);
-  
-  if (x->db) {
+  if((x->db = audiodb_open(x->x_dbname->s_name, O_RDWR))) {
     post("Opened");
   } else {
     error("Could not open db '%s'.", x->x_dbname->s_name);
   }
 }
 
+static void adbpd_close(t_adbpd *x) {
+  if(x->db) {
+    post("Closing db");
+    audiodb_close(x->db);
+    x->db = NULL;
+  }
+}
+
 /* This is accessed via the 'set' message. It sets the name and opens
    the database. */
 static void adbpd_setname(t_adbpd *x, t_symbol *s, int argc, t_atom *argv) {
@@ -416,6 +423,7 @@
   class_addmethod(adbpd_class, (t_method)adbpd_getname, gensym("get"), A_NULL);
   class_addmethod(adbpd_class, (t_method)adbpd_create, gensym("create"), A_FLOAT, A_FLOAT, A_FLOAT, 0);
   class_addmethod(adbpd_class, (t_method)adbpd_open, gensym("open"), A_NULL);
+  class_addmethod(adbpd_class, (t_method)adbpd_close, gensym("close"), A_NULL);
   class_addmethod(adbpd_class, (t_method)adbpd_status, gensym("status"), A_NULL);
   class_addmethod(adbpd_class, (t_method)adbpd_l2norm, gensym("l2norm"), A_NULL);
   class_addmethod(adbpd_class, (t_method)adbpd_power, gensym("power"), A_NULL);