From 79e7dc7e7ccd3a824f48730473211eeb7a5fc5d3 Mon Sep 17 00:00:00 2001
From: Ivica Ico Bukvic <ico@vt.edu>
Date: Sun, 14 Sep 2014 13:37:13 -0400
Subject: [PATCH] *fixed valgrind memory leak errors. *TODO: why is alsa's
 snd_ctl_card_info_free(info) causing double free error (valgrind's "Adress 
 is is 40 bytes inside a block of size 376 free'd)? No clue. But with that
 call disabled, everything works as it should and valgrind reports no further
 errors.

---
 pd/src/g_text.c       | 9 +++++----
 pd/src/m_memory.c     | 2 +-
 pd/src/s_audio_alsa.c | 6 +++---
 3 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/pd/src/g_text.c b/pd/src/g_text.c
index 67ea85a37..9d12259b5 100644
--- a/pd/src/g_text.c
+++ b/pd/src/g_text.c
@@ -2357,10 +2357,11 @@ void text_setto(t_text *x, t_glist *glist, char *buf, int bufsize, int pos)
                 canvas_updatewindowlist();
     }
     else { // T_MESSAGE, T_TEXT, T_ATOM
-        if (x->te_type == T_TEXT)
+        if (buf && x->te_type == T_TEXT)
         {
-            char * c;
-            for(c = buf; *c != '\0'; c++)
+            char *c;
+            int n;
+            for(c = buf, n = 0; n < bufsize; n++, c++)
             {
                 if(*c == '\n')
                 {
@@ -2372,7 +2373,7 @@ void text_setto(t_text *x, t_glist *glist, char *buf, int bufsize, int pos)
         t_binbuf *b = binbuf_new();
         binbuf_text(b, buf, bufsize);
         binbuf_gettext(b, &c2, &i2);
-        if (strcmp(c1, c2))
+        if (!c1 || strcmp(c1, c2))
         {
             canvas_undo_add(glist_getcanvas(glist), 10, "typing",
                 (void *)canvas_undo_set_recreate(glist_getcanvas(glist),
diff --git a/pd/src/m_memory.c b/pd/src/m_memory.c
index 39d77babc..2b2530017 100644
--- a/pd/src/m_memory.c
+++ b/pd/src/m_memory.c
@@ -55,7 +55,7 @@ void *resizebytes(void *old, size_t oldsize, size_t newsize)
     void *ret;
     if (newsize < 1) newsize = 1;
     if (oldsize < 1) oldsize = 1;
-    ret = (void *)realloc((char *)old, newsize);
+    ret = (void *)realloc((char *)old, newsize * sizeof(char *));
     if (newsize > oldsize && ret)
         memset(((char *)ret) + oldsize, 0, newsize - oldsize);
 #ifdef LOUD
diff --git a/pd/src/s_audio_alsa.c b/pd/src/s_audio_alsa.c
index 7366c0eff..88dc9b74c 100644
--- a/pd/src/s_audio_alsa.c
+++ b/pd/src/s_audio_alsa.c
@@ -847,14 +847,14 @@ void alsa_getdevs(char *indevlist, int *nindevs,
             /* apparently, "cardno" is just a counter; but check that here */
         if (ndev != cardno)
             fprintf(stderr, "oops: ALSA cards not reported in order?\n");
-        sprintf(devname, "hw:%d", cardno );
-        /* fprintf(stderr, "\ntry %s...\n", devname); */
+        sprintf(devname, "hw:%d", cardno);
+        // fprintf(stderr, "\ntry %s...\n", devname);
         if (snd_ctl_open(&ctl, devname, 0) >= 0)
         {
             snd_ctl_card_info_malloc(&info);
             snd_ctl_card_info(ctl, info);
             desc = snd_ctl_card_info_get_name(info);
-            snd_ctl_card_info_free(info);
+            //snd_ctl_card_info_free(info);
         }
         else
         {
-- 
GitLab