# HG changeset patch # User bsmith@81767d24-ef19-dc11-ae90-00e081727c95 # Date 1341081259 0 # Node ID 5ffeea4a2a4ba858a61a2b242a560af305cfbe98 # Parent d894f87387b2c3d812943d04488a8ca98491769d 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. diff -r d894f87387b2 -r 5ffeea4a2a4b mac/dw.m --- 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;zcount;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;zcount;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;zcount > 1) { - tmpitem[z] = thisitem[z]; - } - for(z=index+1;zcount;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;zcount;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;zcount > 1) { - tmpitem[z] = thisitem[z]; - } - for(z=index+1;zcount;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 && zcount;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;zcount;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;zcount > 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;zcount;z++) + { + tmpitem[z-1] = thisitem[z]; + } } - for(z=index+1;zcount;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--;