comctl32: tooltips: Avoid memory leak when changing tool text

Oleg Krylov oleg.krylov at gmail.com
Wed Oct 4 13:27:27 CDT 2006


There is a memory leak in tooltip code that if tooltext was set as a 
string then changing it to resource ID will not free allocated string.

Changelog:
- comctl32: tooltips: Avoid memory leak when changing tool text
Author: Oleg Krylov
License: LGPL
-------------- next part --------------
>From 0042cc6197ae4ee9d3588aa9e455503f540a3828 Mon Sep 17 00:00:00 2001
From: Oleg Krylov <oleg.krylov at gmail.com>
Date: Wed, 4 Oct 2006 18:11:28 +0300
Subject: comctl32: tooltips: Avoid memory leak when changing tool text and remove some redundant checks

---
 dlls/comctl32/tooltips.c |   98 +++++++++++++++++++++++-----------------------
 1 files changed, 49 insertions(+), 49 deletions(-)

diff --git a/dlls/comctl32/tooltips.c b/dlls/comctl32/tooltips.c
index 83064f2..0643599 100644
--- a/dlls/comctl32/tooltips.c
+++ b/dlls/comctl32/tooltips.c
@@ -2015,6 +2015,14 @@ TOOLTIPS_SetToolInfoA (HWND hwnd, WPARAM
     toolPtr->rect   = lpToolInfo->rect;
     toolPtr->hinst  = lpToolInfo->hinst;
 
+    /* free previous tool text */
+    if (toolPtr->lpszText) {
+	if ( (toolPtr->lpszText != LPSTR_TEXTCALLBACKW) &&
+	     !IS_INTRESOURCE(toolPtr->lpszText) )
+	    Free (toolPtr->lpszText);
+        toolPtr->lpszText = NULL;
+    }
+
     if (IS_INTRESOURCE(lpToolInfo->lpszText)) {
 	TRACE("set string id %x\n", LOWORD(lpToolInfo->lpszText));
 	toolPtr->lpszText = (LPWSTR)lpToolInfo->lpszText;
@@ -2023,19 +2031,11 @@ TOOLTIPS_SetToolInfoA (HWND hwnd, WPARAM
 	if (lpToolInfo->lpszText == LPSTR_TEXTCALLBACKA)
 	    toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
 	else {
-	    if ( (toolPtr->lpszText) &&
-		 !IS_INTRESOURCE(toolPtr->lpszText) ) {
-		if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
-                    Free (toolPtr->lpszText);
-		toolPtr->lpszText = NULL;
-	    }
-	    if (lpToolInfo->lpszText) {
-		INT len = MultiByteToWideChar(CP_ACP, 0, lpToolInfo->lpszText,
-					      -1, NULL, 0);
-		toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
-		MultiByteToWideChar(CP_ACP, 0, lpToolInfo->lpszText, -1,
-				    toolPtr->lpszText, len);
-	    }
+            INT len = MultiByteToWideChar(CP_ACP, 0, lpToolInfo->lpszText,
+                                          -1, NULL, 0);
+            toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
+            MultiByteToWideChar(CP_ACP, 0, lpToolInfo->lpszText, -1,
+                                toolPtr->lpszText, len);
 	}
     }
 
@@ -2073,25 +2073,25 @@ TOOLTIPS_SetToolInfoW (HWND hwnd, WPARAM
     toolPtr->rect   = lpToolInfo->rect;
     toolPtr->hinst  = lpToolInfo->hinst;
 
+    /* free previous tool text */
+    if (toolPtr->lpszText) {
+	if ( (toolPtr->lpszText != LPSTR_TEXTCALLBACKW) &&
+	     !IS_INTRESOURCE(toolPtr->lpszText) )
+	    Free (toolPtr->lpszText);
+        toolPtr->lpszText = NULL;
+    }
+
     if (IS_INTRESOURCE(lpToolInfo->lpszText)) {
 	TRACE("set string id %x!\n", LOWORD(lpToolInfo->lpszText));
 	toolPtr->lpszText = lpToolInfo->lpszText;
     }
-    else {
+    else if (lpToolInfo->lpszText) {
 	if (lpToolInfo->lpszText == LPSTR_TEXTCALLBACKW)
 	    toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
 	else {
-	    if ( (toolPtr->lpszText) &&
-		 !IS_INTRESOURCE(toolPtr->lpszText) ) {
-		if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
-                    Free (toolPtr->lpszText);
-		toolPtr->lpszText = NULL;
-	    }
-	    if (lpToolInfo->lpszText) {
-		INT len = lstrlenW (lpToolInfo->lpszText);
-		toolPtr->lpszText = Alloc ((len+1)*sizeof(WCHAR));
-		strcpyW (toolPtr->lpszText, lpToolInfo->lpszText);
-	    }
+            INT len = lstrlenW (lpToolInfo->lpszText);
+            toolPtr->lpszText = Alloc ((len+1)*sizeof(WCHAR));
+            strcpyW (toolPtr->lpszText, lpToolInfo->lpszText);
 	}
     }
 
@@ -2201,6 +2201,14 @@ TOOLTIPS_UpdateTipTextA (HWND hwnd, WPAR
     /* copy tool text */
     toolPtr->hinst  = lpToolInfo->hinst;
 
+    /* free previous tool text */
+    if (toolPtr->lpszText) {
+	if ( (toolPtr->lpszText != LPSTR_TEXTCALLBACKW) &&
+	     !IS_INTRESOURCE(toolPtr->lpszText) )
+	    Free (toolPtr->lpszText);
+        toolPtr->lpszText = NULL;
+    }
+
     if (IS_INTRESOURCE(lpToolInfo->lpszText)){
 	toolPtr->lpszText = (LPWSTR)lpToolInfo->lpszText;
     }
@@ -2208,19 +2216,11 @@ TOOLTIPS_UpdateTipTextA (HWND hwnd, WPAR
 	if (lpToolInfo->lpszText == LPSTR_TEXTCALLBACKA)
 	    toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
 	else {
-	    if ( (toolPtr->lpszText) &&
-		 !IS_INTRESOURCE(toolPtr->lpszText) ) {
-		if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
-                    Free (toolPtr->lpszText);
-		toolPtr->lpszText = NULL;
-	    }
-	    if (lpToolInfo->lpszText) {
-		INT len = MultiByteToWideChar(CP_ACP, 0, lpToolInfo->lpszText,
-					      -1, NULL, 0);
-		toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
-		MultiByteToWideChar(CP_ACP, 0, lpToolInfo->lpszText, -1,
-				    toolPtr->lpszText, len);
-	    }
+            INT len = MultiByteToWideChar(CP_ACP, 0, lpToolInfo->lpszText,
+                                          -1, NULL, 0);
+            toolPtr->lpszText = Alloc (len * sizeof(WCHAR));
+            MultiByteToWideChar(CP_ACP, 0, lpToolInfo->lpszText, -1,
+                                toolPtr->lpszText, len);
 	}
     }
 
@@ -2259,6 +2259,14 @@ TOOLTIPS_UpdateTipTextW (HWND hwnd, WPAR
     /* copy tool text */
     toolPtr->hinst  = lpToolInfo->hinst;
 
+    /* free previous tool text */
+    if (toolPtr->lpszText) {
+	if ( (toolPtr->lpszText != LPSTR_TEXTCALLBACKW) &&
+	     !IS_INTRESOURCE(toolPtr->lpszText) )
+	    Free (toolPtr->lpszText);
+        toolPtr->lpszText = NULL;
+    }
+
     if (IS_INTRESOURCE(lpToolInfo->lpszText)){
 	toolPtr->lpszText = lpToolInfo->lpszText;
     }
@@ -2266,17 +2274,9 @@ TOOLTIPS_UpdateTipTextW (HWND hwnd, WPAR
 	if (lpToolInfo->lpszText == LPSTR_TEXTCALLBACKW)
 	    toolPtr->lpszText = LPSTR_TEXTCALLBACKW;
 	else {
-	    if ( (toolPtr->lpszText)  &&
-		 !IS_INTRESOURCE(toolPtr->lpszText) ) {
-		if( toolPtr->lpszText != LPSTR_TEXTCALLBACKW)
-                    Free (toolPtr->lpszText);
-		toolPtr->lpszText = NULL;
-	    }
-	    if (lpToolInfo->lpszText) {
-		INT len = lstrlenW (lpToolInfo->lpszText);
-		toolPtr->lpszText = Alloc ((len+1)*sizeof(WCHAR));
-		strcpyW (toolPtr->lpszText, lpToolInfo->lpszText);
-	    }
+            INT len = lstrlenW (lpToolInfo->lpszText);
+            toolPtr->lpszText = Alloc ((len+1)*sizeof(WCHAR));
+            strcpyW (toolPtr->lpszText, lpToolInfo->lpszText);
 	}
     }
 
-- 
1.4.2.1



More information about the wine-patches mailing list