Mercurial > dwindows
comparison win/dw.c @ 1762:b472a892ce1f
Added code to plug several resource leaks on Windows.
The GetIconInfo() leak is fixed but tooltips and bitmap buttons
are still leaking fonts and icons respectively... even though
I have verified the code to delete them is executing.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Wed, 27 Jun 2012 20:50:31 +0000 |
parents | ae7f1a0eb0ea |
children | d894f87387b2 |
comparison
equal
deleted
inserted
replaced
1761:ae7f1a0eb0ea | 1762:b472a892ce1f |
---|---|
717 return SignalTranslate[z].message; | 717 return SignalTranslate[z].message; |
718 } | 718 } |
719 return 0L; | 719 return 0L; |
720 } | 720 } |
721 | 721 |
722 /* This function removes and handlers on windows and frees | 722 /* This function removes any handlers on windows and frees |
723 * the user memory allocated to it. | 723 * the user memory and resources allocated to it. |
724 */ | 724 */ |
725 BOOL CALLBACK _free_window_memory(HWND handle, LPARAM lParam) | 725 BOOL CALLBACK _free_window_memory(HWND handle, LPARAM lParam) |
726 { | 726 { |
727 ColorInfo *thiscinfo = (ColorInfo *)GetWindowLongPtr(handle, GWLP_USERDATA); | 727 ColorInfo *thiscinfo = (ColorInfo *)GetWindowLongPtr(handle, GWLP_USERDATA); |
728 HFONT oldfont = (HFONT)SendMessage(handle, WM_GETFONT, 0, 0); | 728 HFONT oldfont = (HFONT)SendMessage(handle, WM_GETFONT, 0, 0); |
729 HICON oldicon = (HICON)SendMessage(handle, WM_GETICON, 0, 0); | 729 HICON oldicon = (HICON)SendMessage(handle, WM_GETICON, 0, 0); |
730 TCHAR tmpbuf[100] = {0}; | 730 TCHAR tmpbuf[100] = {0}; |
731 | 731 HWND tooltip = (HWND)dw_window_get_data(handle, "_dw_tooltip"); |
732 | |
733 if(tooltip) | |
734 DestroyWindow(tooltip); | |
735 | |
732 GetClassName(handle, tmpbuf, 99); | 736 GetClassName(handle, tmpbuf, 99); |
733 | 737 |
734 /* Don't try to free memory from an OLE embedded IE */ | 738 /* Don't try to free memory from an OLE embedded IE */ |
735 if(_tcsncmp(tmpbuf, TEXT("Internet Explorer_Server"), 25) == 0) | 739 if(_tcsncmp(tmpbuf, TEXT("Internet Explorer_Server"), 25) == 0) |
736 return TRUE; | 740 return TRUE; |
749 DeleteObject(oldbitmap); | 753 DeleteObject(oldbitmap); |
750 } | 754 } |
751 if(_tcsnicmp(tmpbuf, BUTTONCLASSNAME, _tcslen(BUTTONCLASSNAME)+1)==0) | 755 if(_tcsnicmp(tmpbuf, BUTTONCLASSNAME, _tcslen(BUTTONCLASSNAME)+1)==0) |
752 { | 756 { |
753 HBITMAP oldbitmap = (HBITMAP)SendMessage(handle, BM_GETIMAGE, IMAGE_BITMAP, 0); | 757 HBITMAP oldbitmap = (HBITMAP)SendMessage(handle, BM_GETIMAGE, IMAGE_BITMAP, 0); |
758 HICON oldicon = (HICON)SendMessage(handle, BM_GETIMAGE, IMAGE_ICON, 0); | |
754 | 759 |
755 if(oldbitmap) | 760 if(oldbitmap) |
756 DeleteObject(oldbitmap); | 761 DeleteObject(oldbitmap); |
762 if(oldicon) | |
763 DestroyIcon(oldicon); | |
757 } | 764 } |
758 #ifdef TOOLBAR | 765 #ifdef TOOLBAR |
759 /* Bitmap Buttons */ | 766 /* Bitmap Buttons */ |
760 else if(_tcsnicmp(tmpbuf, TOOLBARCLASSNAME, _tcslen(TOOLBARCLASSNAME)+1) == 0) | 767 else if(_tcsnicmp(tmpbuf, TOOLBARCLASSNAME, _tcslen(TOOLBARCLASSNAME)+1) == 0) |
761 { | 768 { |
4572 char *buf = dw_window_get_text(handle); | 4579 char *buf = dw_window_get_text(handle); |
4573 TCHAR tmpbuf[100] = {0}; | 4580 TCHAR tmpbuf[100] = {0}; |
4574 static char testtext[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | 4581 static char testtext[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
4575 HBITMAP hbm = 0; | 4582 HBITMAP hbm = 0; |
4576 HICON hic = 0; | 4583 HICON hic = 0; |
4584 ICONINFO ii = {0}; | |
4577 | 4585 |
4578 GetClassName(handle, tmpbuf, 99); | 4586 GetClassName(handle, tmpbuf, 99); |
4579 | 4587 |
4580 /* If we have a string... | 4588 /* If we have a string... |
4581 * calculate the size with the current font. | 4589 * calculate the size with the current font. |
4594 hic = (HICON)SendMessage(handle, BM_GETIMAGE, IMAGE_ICON, 0); | 4602 hic = (HICON)SendMessage(handle, BM_GETIMAGE, IMAGE_ICON, 0); |
4595 | 4603 |
4596 /* If we got an icon, pull out the internal bitmap */ | 4604 /* If we got an icon, pull out the internal bitmap */ |
4597 if(hic) | 4605 if(hic) |
4598 { | 4606 { |
4599 ICONINFO ii; | |
4600 | |
4601 if(GetIconInfo(hic, &ii)) | 4607 if(GetIconInfo(hic, &ii)) |
4602 hbm = ii.hbmMask ? ii.hbmMask : ii.hbmColor; | 4608 hbm = ii.hbmMask ? ii.hbmMask : ii.hbmColor; |
4603 } | 4609 } |
4604 | 4610 |
4605 /* If we weren't able to get the bitmap from the icon... */ | 4611 /* If we weren't able to get the bitmap from the icon... */ |
4838 /* Set the requested sizes */ | 4844 /* Set the requested sizes */ |
4839 if(width) | 4845 if(width) |
4840 *width = thiswidth + extrawidth; | 4846 *width = thiswidth + extrawidth; |
4841 if(height) | 4847 if(height) |
4842 *height = thisheight + extraheight; | 4848 *height = thisheight + extraheight; |
4849 | |
4850 /* Free temporary bitmaps */ | |
4851 if(ii.hbmColor) | |
4852 DeleteObject(ii.hbmColor); | |
4853 if(ii.hbmMask); | |
4854 DeleteObject(ii.hbmMask); | |
4843 } | 4855 } |
4844 | 4856 |
4845 /* | 4857 /* |
4846 * Sets the font used by a specified window (widget) handle. | 4858 * Sets the font used by a specified window (widget) handle. |
4847 * Parameters: | 4859 * Parameters: |
6186 SetWindowLongPtr(tmp, GWLP_USERDATA, (LONG_PTR)cinfo); | 6198 SetWindowLongPtr(tmp, GWLP_USERDATA, (LONG_PTR)cinfo); |
6187 dw_window_set_font(tmp, DefaultFont); | 6199 dw_window_set_font(tmp, DefaultFont); |
6188 return tmp; | 6200 return tmp; |
6189 } | 6201 } |
6190 | 6202 |
6203 #ifdef TOOLBAR | |
6191 /* Internal function to create a grayscale bitmap from a color one */ | 6204 /* Internal function to create a grayscale bitmap from a color one */ |
6192 void _to_grayscale(HBITMAP hbm, int width, int height) | 6205 void _to_grayscale(HBITMAP hbm, int width, int height) |
6193 { | 6206 { |
6194 HDC hdc = CreateCompatibleDC(NULL); | 6207 HDC hdc = CreateCompatibleDC(NULL); |
6195 if (hdc) | 6208 if (hdc) |
6230 imlist = ImageList_Create(bmi.bmWidth, bmi.bmHeight, ILC_COLOR32, 1, 0); | 6243 imlist = ImageList_Create(bmi.bmWidth, bmi.bmHeight, ILC_COLOR32, 1, 0); |
6231 ImageList_Add(imlist, hbitmap, NULL); | 6244 ImageList_Add(imlist, hbitmap, NULL); |
6232 dimlist = ImageList_Create(bmi.bmWidth, bmi.bmHeight, ILC_COLOR32, 1, 0); | 6245 dimlist = ImageList_Create(bmi.bmWidth, bmi.bmHeight, ILC_COLOR32, 1, 0); |
6233 _to_grayscale(hbitmap, bmi.bmWidth, bmi.bmHeight); | 6246 _to_grayscale(hbitmap, bmi.bmWidth, bmi.bmHeight); |
6234 ImageList_Add(dimlist, hbitmap, NULL); | 6247 ImageList_Add(dimlist, hbitmap, NULL); |
6248 DeleteObject(hbitmap); | |
6235 } | 6249 } |
6236 else if(icon) | 6250 else if(icon) |
6237 { | 6251 { |
6238 ICONINFO iconinfo; | 6252 ICONINFO iconinfo; |
6239 | 6253 |
6242 imlist = ImageList_Create(bmi.bmWidth, bmi.bmHeight, ILC_COLOR32 | ILC_MASK, 1, 0); | 6256 imlist = ImageList_Create(bmi.bmWidth, bmi.bmHeight, ILC_COLOR32 | ILC_MASK, 1, 0); |
6243 ImageList_AddIcon(imlist, icon); | 6257 ImageList_AddIcon(imlist, icon); |
6244 dimlist = ImageList_Create(bmi.bmWidth, bmi.bmHeight, ILC_COLOR32 | ILC_MASK, 1, 0); | 6258 dimlist = ImageList_Create(bmi.bmWidth, bmi.bmHeight, ILC_COLOR32 | ILC_MASK, 1, 0); |
6245 _to_grayscale(iconinfo.hbmColor, bmi.bmWidth, bmi.bmHeight); | 6259 _to_grayscale(iconinfo.hbmColor, bmi.bmWidth, bmi.bmHeight); |
6246 ImageList_Add(dimlist, iconinfo.hbmColor, iconinfo.hbmMask); | 6260 ImageList_Add(dimlist, iconinfo.hbmColor, iconinfo.hbmMask); |
6261 DeleteObject(iconinfo.hbmColor); | |
6262 DeleteObject(iconinfo.hbmMask); | |
6263 DestroyIcon(icon); | |
6247 } | 6264 } |
6248 else | 6265 else |
6249 return 0; | 6266 return 0; |
6250 | 6267 |
6251 /* Create the toolbar */ | 6268 /* Create the toolbar */ |
6265 SendMessage(tmp, TB_ADDBUTTONS, 1, (LONG) &tbButtons); | 6282 SendMessage(tmp, TB_ADDBUTTONS, 1, (LONG) &tbButtons); |
6266 | 6283 |
6267 _create_tooltip(tmp, text); | 6284 _create_tooltip(tmp, text); |
6268 return tmp; | 6285 return tmp; |
6269 } | 6286 } |
6287 #endif | |
6270 | 6288 |
6271 /* | 6289 /* |
6272 * Create a new bitmap button window (widget) to be packed. | 6290 * Create a new bitmap button window (widget) to be packed. |
6273 * Parameters: | 6291 * Parameters: |
6274 * text: Bubble help text to be displayed. | 6292 * text: Bubble help text to be displayed. |
9018 * handle: Handle to the container to be configured. | 9036 * handle: Handle to the container to be configured. |
9019 * title: The title to be displayed in the main column. | 9037 * title: The title to be displayed in the main column. |
9020 */ | 9038 */ |
9021 void API dw_filesystem_set_column_title(HWND handle, char *title) | 9039 void API dw_filesystem_set_column_title(HWND handle, char *title) |
9022 { | 9040 { |
9023 char *newtitle = strdup(title ? title : ""); | 9041 char *newtitle = _strdup(title ? title : ""); |
9024 | 9042 |
9025 dw_window_set_data(handle, "_dw_coltitle", newtitle); | 9043 dw_window_set_data(handle, "_dw_coltitle", newtitle); |
9026 } | 9044 } |
9027 | 9045 |
9028 /* | 9046 /* |
11603 char *src = calloc(len + 1, 1); | 11621 char *src = calloc(len + 1, 1); |
11604 | 11622 |
11605 memcpy(src, str, len); | 11623 memcpy(src, str, len); |
11606 buf = UTF8toWide(src); | 11624 buf = UTF8toWide(src); |
11607 free(src); | 11625 free(src); |
11608 len = _tcslen(buf); | 11626 len = (int)_tcslen(buf); |
11609 #else | 11627 #else |
11610 int type = CF_TEXT; | 11628 int type = CF_TEXT; |
11611 | 11629 |
11612 buf = str; | 11630 buf = str; |
11613 #endif | 11631 #endif |