From 495b21fa0cb3d782ff61e53fe583e7f8b2045fbe Mon Sep 17 00:00:00 2001 From: Dalai Felinto Date: Mon, 2 Apr 2018 18:49:00 -0300 Subject: [PATCH] Move to Collection: Reduce memory leakage This is really minor but anyways, now it will only leak if you cancel the menu. And that only if htis is the last time you called this operator before closing Blender. --- source/blender/editors/object/object_edit.c | 29 ++++++++++++++------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/source/blender/editors/object/object_edit.c b/source/blender/editors/object/object_edit.c index a80a738f08f..0506225769c 100644 --- a/source/blender/editors/object/object_edit.c +++ b/source/blender/editors/object/object_edit.c @@ -2150,17 +2150,28 @@ static int move_to_collection_menus_create(wmOperator *op, MoveToCollectionData return index; } -static void move_to_collection_menus_free(MoveToCollectionData *menu) +static void move_to_collection_menus_free_recursive(MoveToCollectionData *menu) { for (MoveToCollectionData *submenu = menu->submenus.first; submenu != NULL; submenu = submenu->next) { - move_to_collection_menus_free(submenu); + move_to_collection_menus_free_recursive(submenu); } BLI_freelistN(&menu->submenus); } +static void move_to_collection_menus_free(MoveToCollectionData **menu) +{ + if (*menu == NULL) { + return; + } + + move_to_collection_menus_free_recursive(*menu); + MEM_freeN(*menu); + *menu = NULL; +} + static void move_to_collection_menu_create(bContext *UNUSED(C), uiLayout *layout, void *menu_v) { MoveToCollectionData *menu = menu_v; @@ -2216,10 +2227,15 @@ static void move_to_collection_menus_items(uiLayout *layout, MoveToCollectionDat } } +/* This is allocated statically because we need this available for the menus creation callback. */ +static MoveToCollectionData *master_collection_menu = NULL; + static int move_to_collection_invoke(bContext *C, wmOperator *op, const wmEvent *event) { - PropertyRNA *prop; + /* Reset the menus data for the current master collection, and free previously allocated data. */ + move_to_collection_menus_free(&master_collection_menu); + PropertyRNA *prop; prop = RNA_struct_find_property(op->ptr, "collection_index"); if (RNA_property_is_set(op->ptr, prop)) { int collection_index = RNA_property_int_get(op->ptr, prop); @@ -2248,16 +2264,11 @@ static int move_to_collection_invoke(bContext *C, wmOperator *op, const wmEvent * called to an operator that exit with OPERATOR_INTERFACE to launch a menu. * * So we are left with a memory that will necessarily leak. It's a small leak though.*/ - static MoveToCollectionData *master_collection_menu = NULL; - if (master_collection_menu == NULL) { master_collection_menu = MEM_callocN(sizeof(MoveToCollectionData), - "MoveToCollectionData menu - expected memleak"); + "MoveToCollectionData menu - expected eventual memleak"); } - /* Reset the menus data for the current master collection, and free previously allocated data. */ - move_to_collection_menus_free(master_collection_menu); - master_collection_menu->collection = master_collection; master_collection_menu->ot = op->type; move_to_collection_menus_create(op, master_collection_menu);