[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [43586] trunk/blender/source/blender/ editors/interface: fix for memory leak displaying shortcuts to buttons which use allocated string , also de-duplocate this code which had this error in 2 places.

Campbell Barton ideasman42 at gmail.com
Sat Jan 21 23:42:17 CET 2012


Revision: 43586
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=43586
Author:   campbellbarton
Date:     2012-01-21 22:42:09 +0000 (Sat, 21 Jan 2012)
Log Message:
-----------
fix for memory leak displaying shortcuts to buttons which use allocated string, also de-duplocate this code which had this error in 2 places.

noticed while testing 1023 length paths.

Modified Paths:
--------------
    trunk/blender/source/blender/editors/interface/interface.c
    trunk/blender/source/blender/editors/interface/interface_handlers.c
    trunk/blender/source/blender/editors/interface/interface_intern.h

Modified: trunk/blender/source/blender/editors/interface/interface.c
===================================================================
--- trunk/blender/source/blender/editors/interface/interface.c	2012-01-21 22:00:40 UTC (rev 43585)
+++ trunk/blender/source/blender/editors/interface/interface.c	2012-01-21 22:42:09 UTC (rev 43586)
@@ -800,11 +800,43 @@
 	}
 }
 
+/* XXX, this code will shorten any allocated string to 'UI_MAX_NAME_STR'
+ * since this is really long its unlikely to be an issue,
+ * but this could be supported */
+void ui_but_add_shortcut(uiBut *but, const char *shortcut_str, const short do_strip)
+{
 
+	if (do_strip) {
+		char *cpoin= strchr(but->str, '|');
+		if(cpoin) {
+			*cpoin= '\0';
+		}
+	}
+
+	/* without this, just allow stripping of the shortcut */
+	if (shortcut_str) {
+		char *butstr_orig;
+
+		if (but->str != but->strdata) {
+			butstr_orig = but->str; /* free after using as source buffer */
+		}
+		else {
+			butstr_orig = BLI_strdup(but->str);
+		}
+		BLI_snprintf(but->strdata,
+		             sizeof(but->strdata),
+		             "%s|%s",
+		             butstr_orig, shortcut_str);
+		MEM_freeN(butstr_orig);
+		but->str = but->strdata;
+		ui_check_but(but);
+	}
+}
+
 static void ui_menu_block_set_keymaps(const bContext *C, uiBlock *block)
 {
 	uiBut *but;
-	char buf[512];
+	char buf[128];
 
 	/* for menu's */
 	MenuType *mt;
@@ -815,18 +847,6 @@
 	if(block->minx != block->maxx)
 		return;
 
-
-#define UI_MENU_KEY_STR_CAT                                                   \
-	char *butstr_orig= BLI_strdup(but->str);                                  \
-	BLI_snprintf(but->strdata,                                                \
-				 sizeof(but->strdata),                                        \
-				 "%s|%s",                                                     \
-				 butstr_orig, buf);                                           \
-	MEM_freeN(butstr_orig);                                                   \
-	but->str= but->strdata;                                                   \
-	ui_check_but(but);                                                        \
-
-
 	for(but=block->buttons.first; but; but=but->next) {
 		if(but->optype) {
 			IDProperty *prop= (but->opptr)? but->opptr->data: NULL;
@@ -834,7 +854,7 @@
 			if(WM_key_event_operator_string(C, but->optype->idname, but->opcontext, prop, TRUE,
 			                                buf, sizeof(buf)))
 			{
-				UI_MENU_KEY_STR_CAT
+				ui_but_add_shortcut(but, buf, FALSE);
 			}
 		}
 		else if ((mt= uiButGetMenuType(but))) {
@@ -851,7 +871,7 @@
 			if(WM_key_event_operator_string(C, "WM_OT_call_menu", WM_OP_INVOKE_REGION_WIN, prop_menu, FALSE,
 			                                buf, sizeof(buf)))
 			{
-				UI_MENU_KEY_STR_CAT
+				ui_but_add_shortcut(but, buf, FALSE);
 			}
 		}
 	}

Modified: trunk/blender/source/blender/editors/interface/interface_handlers.c
===================================================================
--- trunk/blender/source/blender/editors/interface/interface_handlers.c	2012-01-21 22:00:40 UTC (rev 43585)
+++ trunk/blender/source/blender/editors/interface/interface_handlers.c	2012-01-21 22:42:09 UTC (rev 43586)
@@ -4382,31 +4382,19 @@
 	uiBut *but = (uiBut *)arg1;
 
 	if (but->optype) {
-		char buf[512], *cpoin;
+		char shortcut_str[128];
 
 		IDProperty *prop= (but->opptr)? but->opptr->data: NULL;
 		
 		/* complex code to change name of button */
 		if(WM_key_event_operator_string(C, but->optype->idname, but->opcontext, prop, TRUE,
-		                                buf, sizeof(buf)))
+		                                shortcut_str, sizeof(shortcut_str)))
 		{
-			char *butstr_orig;
-
-			// XXX but->str changed... should not, remove the hotkey from it
-			cpoin= strchr(but->str, '|');
-			if(cpoin) *cpoin= 0;		
-
-			butstr_orig= BLI_strdup(but->str);
-			BLI_snprintf(but->strdata, sizeof(but->strdata), "%s|%s", butstr_orig, buf);
-			MEM_freeN(butstr_orig);
-			but->str= but->strdata;
-
-			ui_check_but(but);
+			ui_but_add_shortcut(but, shortcut_str, TRUE);
 		}
 		else {
-			/* shortcut was removed */
-			cpoin= strchr(but->str, '|');
-			if(cpoin) *cpoin= 0;
+			/* simply strip the shortcut */
+			ui_but_add_shortcut(but, NULL, TRUE);
 		}
 	}
 }

Modified: trunk/blender/source/blender/editors/interface/interface_intern.h
===================================================================
--- trunk/blender/source/blender/editors/interface/interface_intern.h	2012-01-21 22:00:40 UTC (rev 43585)
+++ trunk/blender/source/blender/editors/interface/interface_intern.h	2012-01-21 22:42:09 UTC (rev 43586)
@@ -498,6 +498,7 @@
 void ui_layout_add_but(uiLayout *layout, uiBut *but);
 int ui_but_can_align(uiBut *but);
 void ui_but_add_search(uiBut *but, PointerRNA *ptr, PropertyRNA *prop, PointerRNA *searchptr, PropertyRNA *searchprop);
+void ui_but_add_shortcut(uiBut *but, const char *key_str, const short do_strip);
 
 /* interface_anim.c */
 void ui_but_anim_flag(uiBut *but, float cfra);



More information about the Bf-blender-cvs mailing list