From 5b97882a6fee6cc6ccb55deeeaf0cc030a3a9538 Mon Sep 17 00:00:00 2001
From: Ivica Ico Bukvic <ico@vt.edu>
Date: Wed, 14 Dec 2011 18:11:13 -0500
Subject: [PATCH] removed rendundant memory hack and found potentially real and
 permanent fix to the double-entry bug (see g_canvas.c inside canvas_free()
 function). Many thanks to Krzysztof Czaja for his incredibly helpful hint!

---
 src/g_canvas.c | 11 ++++++++---
 src/g_canvas.h |  5 ++---
 src/m_pd.c     | 32 +++++++++++++++++---------------
 3 files changed, 27 insertions(+), 21 deletions(-)

diff --git a/src/g_canvas.c b/src/g_canvas.c
index 1615f84e2..aa06ac10c 100644
--- a/src/g_canvas.c
+++ b/src/g_canvas.c
@@ -344,7 +344,7 @@ void glist_init(t_glist *x)
 }
 
 /* global var used by pd_new() to work with redundant memory, originally defined in m_pd.c */
-extern int canvas_check_duplicate;
+//extern int canvas_check_duplicate;
 
     /* make a new glist.  It will either be a "root" canvas or else
     it appears as a "text" object in another window (canvas_getcurrent() 
@@ -357,6 +357,7 @@ t_canvas *canvas_new(void *dummy, t_symbol *sel, int argc, t_atom *argv)
 
 	//if (x->gl_owner && x->gl_env) {
 
+/*
 	t_redundant_mem *new_rm = (t_redundant_mem *)t_getbytes(sizeof(*new_rm));
 	//new_rm->rm_what = (int)getbytes(1);
 	if (rm_start == NULL) {
@@ -376,9 +377,10 @@ t_canvas *canvas_new(void *dummy, t_symbol *sel, int argc, t_atom *argv)
 	}
 	//}
 
-	canvas_check_duplicate = -1;
+
+	canvas_check_duplicate = -1;*/
     t_canvas *x = (t_canvas *)pd_new(canvas_class);
-	rm_end->rm_canvas = x;
+	//rm_end->rm_canvas = x;
     t_canvas *owner = canvas_getcurrent();
     t_symbol *s = &s_;
     int vis = 0, width = GLIST_DEFCANVASWIDTH, height = GLIST_DEFCANVASHEIGHT;
@@ -919,6 +921,9 @@ void canvas_free(t_canvas *x)
     if (x == glist_getcanvas(x))
         canvas_vis(x, 0);
 
+	if (x->gl_editor)
+		canvas_destroy_editor(x);
+
     if (strcmp(x->gl_name->s_name, "Pd")) {
 		//fprintf(stderr,"canvas_free calling pd_unbind\n");
         pd_unbind(&x->gl_pd, canvas_makebindsym(x->gl_name));
diff --git a/src/g_canvas.h b/src/g_canvas.h
index 57a2ca893..c30b6da03 100644
--- a/src/g_canvas.h
+++ b/src/g_canvas.h
@@ -45,8 +45,7 @@ extern "C" {
 	instantiated inside canvas_new since the bug only affects new
 	canvases/abstractions rather than individual objects. the queue
 	is destructed in m_glob.c quit call (when pd exits) */
-
-
+/*
 typedef struct _redundant_mem
 {
     //int rm_what;
@@ -56,7 +55,7 @@ typedef struct _redundant_mem
 
 t_redundant_mem *rm_start;
 t_redundant_mem *rm_end;
-
+*/
   
 /* --------------------- geometry ---------------------------- */
 #define IOWIDTH 7       /* width of an inlet/outlet in pixels */
diff --git a/src/m_pd.c b/src/m_pd.c
index dd9589cdd..951e8dd2b 100644
--- a/src/m_pd.c
+++ b/src/m_pd.c
@@ -11,10 +11,11 @@
 
     /* FIXME no out-of-memory testing yet! */
 
-int canvas_check_duplicate = 0;
+/*int canvas_check_duplicate = 0;
 
 extern t_redundant_mem *rm_start;
 extern t_redundant_mem *rm_end;
+*/
 
 t_pd *pd_new(t_class *c)
 {
@@ -45,7 +46,7 @@ t_pd *pd_new(t_class *c)
 
 	//int random_extra = 0;
 	//int random_offset = 1;
-
+/*
 	while (canvas_check_duplicate != 0) {
 
 		if (canvas_check_duplicate == -1) {
@@ -78,21 +79,22 @@ t_pd *pd_new(t_class *c)
 		}
 
 		//fprintf(stderr,"done alloc %d\n", canvas_check_duplicate);
-		/*if (canvas_check_duplicate == 1) {
-			//fprintf(stderr,"duplicate\n");
-			if (!random_extra)
-				t_freebytes(x, sizeof(c->c_size));
-			else
-				t_freebytes(x, sizeof(c->c_size+random_extra));
-		}
-
-		if (!canvas_check_duplicate && random_extra) {
-			x = (t_pd *)t_resizebytes(x, c->c_size+random_extra, c->c_size);
-			break;
-		}*/
+		//if (canvas_check_duplicate == 1) {
+		//	//fprintf(stderr,"duplicate\n");
+		//	if (!random_extra)
+		//		t_freebytes(x, sizeof(c->c_size));
+		//	else
+		//		t_freebytes(x, sizeof(c->c_size+random_extra));
+		//}
+		//
+		//if (!canvas_check_duplicate && random_extra) {
+		//	x = (t_pd *)t_resizebytes(x, c->c_size+random_extra, c->c_size);
+		//	break;
+		//}
 	}
 
-    if (!x) x = (t_pd *)t_getbytes(c->c_size);
+    if (!x) x = (t_pd *)t_getbytes(c->c_size);*/
+	x = (t_pd *)t_getbytes(c->c_size);
     *x = c;
     if (c->c_patchable)
     {
-- 
GitLab