changeset 1764:5ffeea4a2a4b

Fixed a memory leak in the Mac box code, items were not being freed when destroyed. Also rewrote the box packing/unpacking code to avoid clang/Xcode warnings on Analyze.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sat, 30 Jun 2012 18:34:19 +0000
parents d894f87387b2
children 15414cbe857f
files mac/dw.m
diffstat 1 files changed, 84 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/mac/dw.m	Wed Jun 27 22:14:28 2012 +0000
+++ b/mac/dw.m	Sat Jun 30 18:34:19 2012 +0000
@@ -707,6 +707,8 @@
 -(void)dealloc
 {
     UserData *root = userdata;
+    if(box->items)
+        free(box->items);
     free(box);
     _remove_userdata(&root, NULL, TRUE);
     dw_signal_disconnect_by_window(self);
@@ -3826,13 +3828,15 @@
     }
 
     /* Do some sanity bounds checking */
+    if(!thisitem)
+        thisbox->count = 0;
     if(index < 0)
         index = 0;
     if(index > thisbox->count)
         index = thisbox->count;
 
     /* Duplicate the existing data */
-    tmpitem = malloc(sizeof(Item)*(thisbox->count+1));
+    tmpitem = calloc(sizeof(Item), (thisbox->count+1));
 
     for(z=0;z<thisbox->count;z++)
     {
@@ -3897,7 +3901,7 @@
     _dw_redraw([view window], TRUE);
 
     /* Free the old data */
-    if(thisbox->count)
+    if(thisitem)
        free(thisitem);
     DW_MUTEX_UNLOCK;
 }
@@ -3935,7 +3939,10 @@
             id window = [object window];
             Box *thisbox = [parent box];
             int z, index = -1;
-            Item *tmpitem, *thisitem = thisbox->items;
+            Item *tmpitem = NULL, *thisitem = thisbox->items;
+            
+            if(!thisitem)
+                thisbox->count = 0;
             
             for(z=0;z<thisbox->count;z++)
             {
@@ -3953,21 +3960,28 @@
             [object retain];
             [object removeFromSuperview];
             
-            tmpitem = malloc(sizeof(Item)*(thisbox->count-1));
-            
-            /* Copy all but the current entry to the new list */
-            for(z=0;z<index;z++)
+            if(thisbox->count > 1)
             {
-                tmpitem[z] = thisitem[z];
-            }
-            for(z=index+1;z<thisbox->count;z++)
-            {
-                tmpitem[z-1] = thisitem[z];
+                tmpitem = calloc(sizeof(Item), (thisbox->count-1));
+                
+                /* Copy all but the current entry to the new list */
+                for(z=0;z<index;z++)
+                {
+                    tmpitem[z] = thisitem[z];
+                }
+                for(z=index+1;z<thisbox->count;z++)
+                {
+                    tmpitem[z-1] = thisitem[z];
+                }
             }
             
             thisbox->items = tmpitem;
-            free(thisitem);
-            thisbox->count--;
+            if(thisitem)
+                free(thisitem);
+            if(tmpitem)
+                thisbox->count--;
+            else
+                thisbox->count = 0;
             /* Queue a redraw on the top-level window */
             _dw_redraw(window, TRUE);
         }
@@ -4001,7 +4015,7 @@
         if(thisbox && index > -1 && index < thisbox->count)
         {
             int z;
-            Item *tmpitem, *thisitem = thisbox->items;
+            Item *tmpitem = NULL, *thisitem = thisbox->items;
             
             object = thisitem[index].hwnd;
             
@@ -4011,21 +4025,29 @@
                 [object removeFromSuperview];
             }
             
-            tmpitem = malloc(sizeof(Item)*(thisbox->count-1));
-            
-            /* Copy all but the current entry to the new list */
-            for(z=0;z<index;z++)
+            if(thisbox->count > 1)
             {
-                tmpitem[z] = thisitem[z];
-            }
-            for(z=index+1;z<thisbox->count;z++)
-            {
-                tmpitem[z-1] = thisitem[z];
+                tmpitem = calloc(sizeof(Item), (thisbox->count-1));
+                
+                /* Copy all but the current entry to the new list */
+                for(z=0;thisitem && z<index;z++)
+                {
+                    tmpitem[z] = thisitem[z];
+                }
+                for(z=index+1;z<thisbox->count;z++)
+                {
+                    tmpitem[z-1] = thisitem[z];
+                }
             }
             
             thisbox->items = tmpitem;
-            free(thisitem);
-            thisbox->count--;
+            if(thisitem)
+                free(thisitem);
+            if(tmpitem)
+                thisbox->count--;
+            else 
+                thisbox->count = 0;
+            
             /* Queue a redraw on the top-level window */
             _dw_redraw(window, TRUE);
         }
@@ -8683,8 +8705,11 @@
             id window = [object window];
             Box *thisbox = [parent box];
             int z, index = -1;
-            Item *tmpitem, *thisitem = thisbox->items;
-
+            Item *tmpitem = NULL, *thisitem = thisbox->items;
+
+            if(!thisitem)
+                thisbox->count = 0;
+            
             for(z=0;z<thisbox->count;z++)
             {
                 if(thisitem[z].hwnd == object)
@@ -8700,21 +8725,29 @@
 
             [object removeFromSuperview];
 
-            tmpitem = malloc(sizeof(Item)*(thisbox->count-1));
-
-            /* Copy all but the current entry to the new list */
-            for(z=0;z<index;z++)
+            if(thisbox->count > 1)
             {
-                tmpitem[z] = thisitem[z];
+                tmpitem = calloc(sizeof(Item), (thisbox->count-1));
+                
+                /* Copy all but the current entry to the new list */
+                for(z=0;z<index;z++)
+                {
+                    tmpitem[z] = thisitem[z];
+                }
+                for(z=index+1;z<thisbox->count;z++)
+                {
+                    tmpitem[z-1] = thisitem[z];
+                }
             }
-            for(z=index+1;z<thisbox->count;z++)
-            {
-                tmpitem[z-1] = thisitem[z];
-            }
-
+            
             thisbox->items = tmpitem;
-            free(thisitem);
-            thisbox->count--;
+            if(thisitem)
+                free(thisitem);
+            if(tmpitem)
+                thisbox->count--;
+            else 
+                thisbox->count = 0;
+            
             /* Queue a redraw on the top-level window */
             _dw_redraw(window, TRUE);
         }
@@ -10088,17 +10121,20 @@
          {
             if((bytesread = (int)read(array[z].fd, &command, 1)) < 1)
             {
-               struct _seminfo *newarray;
+               struct _seminfo *newarray = NULL;
 
                /* Remove this connection from the set */
-               newarray = (struct _seminfo *)malloc(sizeof(struct _seminfo)*(connectcount-1));
-               if(!z)
-                  memcpy(newarray, &array[1], sizeof(struct _seminfo)*(connectcount-1));
-               else
+               if(connectcount > 1)
                {
-                  memcpy(newarray, array, sizeof(struct _seminfo)*z);
-                  if(z!=(connectcount-1))
-                     memcpy(&newarray[z], &array[z+1], sizeof(struct _seminfo)*(z-connectcount-1));
+                   newarray = (struct _seminfo *)malloc(sizeof(struct _seminfo)*(connectcount-1));
+                   if(!z)
+                       memcpy(newarray, &array[1], sizeof(struct _seminfo)*(connectcount-1));
+                   else
+                   {
+                       memcpy(newarray, array, sizeof(struct _seminfo)*z);
+                       if(z!=(connectcount-1))
+                           memcpy(&newarray[z], &array[z+1], sizeof(struct _seminfo)*(z-connectcount-1));
+                   }
                }
                connectcount--;