fix for memory leak displaying shortcuts to buttons which use allocated string, also...
authorCampbell Barton <ideasman42@gmail.com>
Sat, 21 Jan 2012 22:42:09 +0000 (22:42 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sat, 21 Jan 2012 22:42:09 +0000 (22:42 +0000)
noticed while testing 1023 length paths.

source/blender/editors/interface/interface.c
source/blender/editors/interface/interface_handlers.c
source/blender/editors/interface/interface_intern.h

index c112918833e34fd949b055960321c7cc8d946955..3a1bab9987e4ef71bf68b3574078e7f554cce2f2 100644 (file)
@@ -800,11 +800,43 @@ static void ui_menu_block_set_keyaccels(uiBlock *block)
        }
 }
 
+/* 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 @@ static void ui_menu_block_set_keymaps(const bContext *C, uiBlock *block)
        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 @@ static void ui_menu_block_set_keymaps(const bContext *C, uiBlock *block)
                        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 @@ static void ui_menu_block_set_keymaps(const bContext *C, uiBlock *block)
                        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);
                        }
                }
        }
index 311f0f87b508deb21b5d66eeec6ab2fb19aaa644..93d8f9c0c8a51392c055a875e1243ec540cfd1f6 100644 (file)
@@ -4382,31 +4382,19 @@ static void but_shortcut_name_func(bContext *C, void *arg1, int UNUSED(event))
        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);
                }
        }
 }
index 2980b28d522b9a228998a0675e533ab00b7aa379..2d8de475c4b8a8f46c53f25690a975d299ea5d88 100644 (file)
@@ -498,6 +498,7 @@ void ui_resources_free(void);
 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);