From 696cddd07e62e88d8749326fb29dc792d2c2e2d8 Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jancsika@yahoo.com>
Date: Wed, 12 Nov 2014 18:49:30 -0500
Subject: [PATCH] fix a memory leak for scalars created in object boxes

---
 pd/src/g_text.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/pd/src/g_text.c b/pd/src/g_text.c
index 81ff821d9..c8d303a42 100644
--- a/pd/src/g_text.c
+++ b/pd/src/g_text.c
@@ -113,6 +113,7 @@ void glist_text(t_glist *gl, t_symbol *s, int argc, t_atom *argv)
 /* ----------------- the "object" object.  ------------------ */
 
 extern t_pd *newest;
+int scalar_in_a_box;
 extern void glist_scalar(t_glist *canvas, t_symbol *s, int argc, t_atom *argv);
 void canvas_getargs(int *argcp, t_atom **argvp);
 
@@ -162,8 +163,9 @@ static void canvas_objtext(t_glist *gl, int xpix, int ypix,
                     glist_scalar(gl, gensym("scalar_from_canvas_objtext"),
                         binbuf_getnatom(b)+2, scalar_create_at);
                     binbuf_free(scalarbuf);
-                    binbuf_free(b);
+                    //binbuf_free(b);
                     canvas_unsetcurrent((t_canvas *)gl);
+                    scalar_in_a_box = 1;
                     return;
                 }
             }
@@ -2382,7 +2384,13 @@ void text_setto(t_text *x, t_glist *glist, char *buf, int bufsize, int pos)
                 canvas_restoreconnections(glist_getcanvas(glist));
                 //canvas_apply_restore_original_position(glist_getcanvas(glist),
                 //    pos);
-                text_checkvalidwidth(glist);
+                /* this conditional is here because I'm creating scalars
+                   inside of object boxes using canvas_objtext.  But scalars
+                   aren't technically t_text, and checkvalidwidth expects
+                   to find a new t_text at the end of the glist.
+                 */
+                if (!scalar_in_a_box)
+                    text_checkvalidwidth(glist);
             }
             else
             {
@@ -2396,8 +2404,16 @@ void text_setto(t_text *x, t_glist *glist, char *buf, int bufsize, int pos)
         if (natom2 >= 1  && vec2[0].a_type == A_SYMBOL
             && !strcmp(vec2[0].a_w.w_symbol->s_name, "pd"))
                 canvas_updatewindowlist();
+        /* this is a quick bugfix-- we need to free the binbuf "b" if we
+           created a scalar in canvas_objtext */
+        if (scalar_in_a_box)
+        {
+            binbuf_free(b);   
+            scalar_in_a_box = 0;
+        }
     }
-    else { // T_MESSAGE, T_TEXT, T_ATOM
+    else
+    { // T_MESSAGE, T_TEXT, T_ATOM
         if (buf && x->te_type == T_TEXT)
         {
             char *c;
-- 
GitLab