comparison mac/dw.m @ 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 e4fcd1e81f0d
children d81bebc5c8cc
comparison
equal deleted inserted replaced
1763:d894f87387b2 1764:5ffeea4a2a4b
705 return self; 705 return self;
706 } 706 }
707 -(void)dealloc 707 -(void)dealloc
708 { 708 {
709 UserData *root = userdata; 709 UserData *root = userdata;
710 if(box->items)
711 free(box->items);
710 free(box); 712 free(box);
711 _remove_userdata(&root, NULL, TRUE); 713 _remove_userdata(&root, NULL, TRUE);
712 dw_signal_disconnect_by_window(self); 714 dw_signal_disconnect_by_window(self);
713 [super dealloc]; 715 [super dealloc];
714 } 716 }
3824 { 3826 {
3825 this = item = [object scrollview]; 3827 this = item = [object scrollview];
3826 } 3828 }
3827 3829
3828 /* Do some sanity bounds checking */ 3830 /* Do some sanity bounds checking */
3831 if(!thisitem)
3832 thisbox->count = 0;
3829 if(index < 0) 3833 if(index < 0)
3830 index = 0; 3834 index = 0;
3831 if(index > thisbox->count) 3835 if(index > thisbox->count)
3832 index = thisbox->count; 3836 index = thisbox->count;
3833 3837
3834 /* Duplicate the existing data */ 3838 /* Duplicate the existing data */
3835 tmpitem = malloc(sizeof(Item)*(thisbox->count+1)); 3839 tmpitem = calloc(sizeof(Item), (thisbox->count+1));
3836 3840
3837 for(z=0;z<thisbox->count;z++) 3841 for(z=0;z<thisbox->count;z++)
3838 { 3842 {
3839 if(z == index) 3843 if(z == index)
3840 x++; 3844 x++;
3895 } 3899 }
3896 /* Queue a redraw on the top-level window */ 3900 /* Queue a redraw on the top-level window */
3897 _dw_redraw([view window], TRUE); 3901 _dw_redraw([view window], TRUE);
3898 3902
3899 /* Free the old data */ 3903 /* Free the old data */
3900 if(thisbox->count) 3904 if(thisitem)
3901 free(thisitem); 3905 free(thisitem);
3902 DW_MUTEX_UNLOCK; 3906 DW_MUTEX_UNLOCK;
3903 } 3907 }
3904 3908
3905 /* 3909 /*
3933 if([parent isKindOfClass:[DWBox class]] || [parent isKindOfClass:[DWGroupBox class]]) 3937 if([parent isKindOfClass:[DWBox class]] || [parent isKindOfClass:[DWGroupBox class]])
3934 { 3938 {
3935 id window = [object window]; 3939 id window = [object window];
3936 Box *thisbox = [parent box]; 3940 Box *thisbox = [parent box];
3937 int z, index = -1; 3941 int z, index = -1;
3938 Item *tmpitem, *thisitem = thisbox->items; 3942 Item *tmpitem = NULL, *thisitem = thisbox->items;
3943
3944 if(!thisitem)
3945 thisbox->count = 0;
3939 3946
3940 for(z=0;z<thisbox->count;z++) 3947 for(z=0;z<thisbox->count;z++)
3941 { 3948 {
3942 if(thisitem[z].hwnd == object) 3949 if(thisitem[z].hwnd == object)
3943 index = z; 3950 index = z;
3951 } 3958 }
3952 3959
3953 [object retain]; 3960 [object retain];
3954 [object removeFromSuperview]; 3961 [object removeFromSuperview];
3955 3962
3956 tmpitem = malloc(sizeof(Item)*(thisbox->count-1)); 3963 if(thisbox->count > 1)
3957
3958 /* Copy all but the current entry to the new list */
3959 for(z=0;z<index;z++)
3960 { 3964 {
3961 tmpitem[z] = thisitem[z]; 3965 tmpitem = calloc(sizeof(Item), (thisbox->count-1));
3962 } 3966
3963 for(z=index+1;z<thisbox->count;z++) 3967 /* Copy all but the current entry to the new list */
3964 { 3968 for(z=0;z<index;z++)
3965 tmpitem[z-1] = thisitem[z]; 3969 {
3970 tmpitem[z] = thisitem[z];
3971 }
3972 for(z=index+1;z<thisbox->count;z++)
3973 {
3974 tmpitem[z-1] = thisitem[z];
3975 }
3966 } 3976 }
3967 3977
3968 thisbox->items = tmpitem; 3978 thisbox->items = tmpitem;
3969 free(thisitem); 3979 if(thisitem)
3970 thisbox->count--; 3980 free(thisitem);
3981 if(tmpitem)
3982 thisbox->count--;
3983 else
3984 thisbox->count = 0;
3971 /* Queue a redraw on the top-level window */ 3985 /* Queue a redraw on the top-level window */
3972 _dw_redraw(window, TRUE); 3986 _dw_redraw(window, TRUE);
3973 } 3987 }
3974 } 3988 }
3975 DW_MUTEX_UNLOCK; 3989 DW_MUTEX_UNLOCK;
3999 Box *thisbox = [parent box]; 4013 Box *thisbox = [parent box];
4000 4014
4001 if(thisbox && index > -1 && index < thisbox->count) 4015 if(thisbox && index > -1 && index < thisbox->count)
4002 { 4016 {
4003 int z; 4017 int z;
4004 Item *tmpitem, *thisitem = thisbox->items; 4018 Item *tmpitem = NULL, *thisitem = thisbox->items;
4005 4019
4006 object = thisitem[index].hwnd; 4020 object = thisitem[index].hwnd;
4007 4021
4008 if(object) 4022 if(object)
4009 { 4023 {
4010 [object retain]; 4024 [object retain];
4011 [object removeFromSuperview]; 4025 [object removeFromSuperview];
4012 } 4026 }
4013 4027
4014 tmpitem = malloc(sizeof(Item)*(thisbox->count-1)); 4028 if(thisbox->count > 1)
4015
4016 /* Copy all but the current entry to the new list */
4017 for(z=0;z<index;z++)
4018 { 4029 {
4019 tmpitem[z] = thisitem[z]; 4030 tmpitem = calloc(sizeof(Item), (thisbox->count-1));
4020 } 4031
4021 for(z=index+1;z<thisbox->count;z++) 4032 /* Copy all but the current entry to the new list */
4022 { 4033 for(z=0;thisitem && z<index;z++)
4023 tmpitem[z-1] = thisitem[z]; 4034 {
4035 tmpitem[z] = thisitem[z];
4036 }
4037 for(z=index+1;z<thisbox->count;z++)
4038 {
4039 tmpitem[z-1] = thisitem[z];
4040 }
4024 } 4041 }
4025 4042
4026 thisbox->items = tmpitem; 4043 thisbox->items = tmpitem;
4027 free(thisitem); 4044 if(thisitem)
4028 thisbox->count--; 4045 free(thisitem);
4046 if(tmpitem)
4047 thisbox->count--;
4048 else
4049 thisbox->count = 0;
4050
4029 /* Queue a redraw on the top-level window */ 4051 /* Queue a redraw on the top-level window */
4030 _dw_redraw(window, TRUE); 4052 _dw_redraw(window, TRUE);
4031 } 4053 }
4032 } 4054 }
4033 DW_MUTEX_UNLOCK; 4055 DW_MUTEX_UNLOCK;
8681 if([parent isKindOfClass:[DWBox class]] || [parent isKindOfClass:[DWGroupBox class]]) 8703 if([parent isKindOfClass:[DWBox class]] || [parent isKindOfClass:[DWGroupBox class]])
8682 { 8704 {
8683 id window = [object window]; 8705 id window = [object window];
8684 Box *thisbox = [parent box]; 8706 Box *thisbox = [parent box];
8685 int z, index = -1; 8707 int z, index = -1;
8686 Item *tmpitem, *thisitem = thisbox->items; 8708 Item *tmpitem = NULL, *thisitem = thisbox->items;
8687 8709
8710 if(!thisitem)
8711 thisbox->count = 0;
8712
8688 for(z=0;z<thisbox->count;z++) 8713 for(z=0;z<thisbox->count;z++)
8689 { 8714 {
8690 if(thisitem[z].hwnd == object) 8715 if(thisitem[z].hwnd == object)
8691 index = z; 8716 index = z;
8692 } 8717 }
8698 return 0; 8723 return 0;
8699 } 8724 }
8700 8725
8701 [object removeFromSuperview]; 8726 [object removeFromSuperview];
8702 8727
8703 tmpitem = malloc(sizeof(Item)*(thisbox->count-1)); 8728 if(thisbox->count > 1)
8704
8705 /* Copy all but the current entry to the new list */
8706 for(z=0;z<index;z++)
8707 { 8729 {
8708 tmpitem[z] = thisitem[z]; 8730 tmpitem = calloc(sizeof(Item), (thisbox->count-1));
8731
8732 /* Copy all but the current entry to the new list */
8733 for(z=0;z<index;z++)
8734 {
8735 tmpitem[z] = thisitem[z];
8736 }
8737 for(z=index+1;z<thisbox->count;z++)
8738 {
8739 tmpitem[z-1] = thisitem[z];
8740 }
8709 } 8741 }
8710 for(z=index+1;z<thisbox->count;z++) 8742
8711 {
8712 tmpitem[z-1] = thisitem[z];
8713 }
8714
8715 thisbox->items = tmpitem; 8743 thisbox->items = tmpitem;
8716 free(thisitem); 8744 if(thisitem)
8717 thisbox->count--; 8745 free(thisitem);
8746 if(tmpitem)
8747 thisbox->count--;
8748 else
8749 thisbox->count = 0;
8750
8718 /* Queue a redraw on the top-level window */ 8751 /* Queue a redraw on the top-level window */
8719 _dw_redraw(window, TRUE); 8752 _dw_redraw(window, TRUE);
8720 } 8753 }
8721 } 8754 }
8722 DW_MUTEX_UNLOCK; 8755 DW_MUTEX_UNLOCK;
10086 { 10119 {
10087 if(FD_ISSET(array[z].fd, &rd)) 10120 if(FD_ISSET(array[z].fd, &rd))
10088 { 10121 {
10089 if((bytesread = (int)read(array[z].fd, &command, 1)) < 1) 10122 if((bytesread = (int)read(array[z].fd, &command, 1)) < 1)
10090 { 10123 {
10091 struct _seminfo *newarray; 10124 struct _seminfo *newarray = NULL;
10092 10125
10093 /* Remove this connection from the set */ 10126 /* Remove this connection from the set */
10094 newarray = (struct _seminfo *)malloc(sizeof(struct _seminfo)*(connectcount-1)); 10127 if(connectcount > 1)
10095 if(!z)
10096 memcpy(newarray, &array[1], sizeof(struct _seminfo)*(connectcount-1));
10097 else
10098 { 10128 {
10099 memcpy(newarray, array, sizeof(struct _seminfo)*z); 10129 newarray = (struct _seminfo *)malloc(sizeof(struct _seminfo)*(connectcount-1));
10100 if(z!=(connectcount-1)) 10130 if(!z)
10101 memcpy(&newarray[z], &array[z+1], sizeof(struct _seminfo)*(z-connectcount-1)); 10131 memcpy(newarray, &array[1], sizeof(struct _seminfo)*(connectcount-1));
10132 else
10133 {
10134 memcpy(newarray, array, sizeof(struct _seminfo)*z);
10135 if(z!=(connectcount-1))
10136 memcpy(&newarray[z], &array[z+1], sizeof(struct _seminfo)*(z-connectcount-1));
10137 }
10102 } 10138 }
10103 connectcount--; 10139 connectcount--;
10104 10140
10105 /* Replace old array with new one */ 10141 /* Replace old array with new one */
10106 free(array); 10142 free(array);