From 236ca8fbe84575504f6ae80aec63c8059b875ef0 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Fri, 24 Jul 2020 12:26:11 +0200 Subject: Allocator: make leak detection work with static variables When definining static variables that own memory, you should use the "construct on first use" idiom. Otherwise, you'll get a warning when Blender exits. More details are provided in D8354. Differential Revision: https://developer.blender.org/D8354 --- source/blender/makesdna/intern/CMakeLists.txt | 1 + source/blender/makesrna/intern/CMakeLists.txt | 1 + source/blender/makesrna/intern/makesrna.c | 12 ++++-------- source/blender/windowmanager/intern/wm_init_exit.c | 7 ------- source/creator/creator.c | 1 + 5 files changed, 7 insertions(+), 15 deletions(-) (limited to 'source') diff --git a/source/blender/makesdna/intern/CMakeLists.txt b/source/blender/makesdna/intern/CMakeLists.txt index 0f2761e311e..737ea9a7e12 100644 --- a/source/blender/makesdna/intern/CMakeLists.txt +++ b/source/blender/makesdna/intern/CMakeLists.txt @@ -40,6 +40,7 @@ set(SRC ../../blenlib/intern/BLI_memarena.c ../../blenlib/intern/BLI_mempool.c ../../blenlib/intern/hash_mm2a.c # needed by 'BLI_ghash_utils.c', not used directly. + ../../../../intern/guardedalloc/intern/leak_detector.cc ../../../../intern/guardedalloc/intern/mallocn.c ../../../../intern/guardedalloc/intern/mallocn_guarded_impl.c ../../../../intern/guardedalloc/intern/mallocn_lockfree_impl.c diff --git a/source/blender/makesrna/intern/CMakeLists.txt b/source/blender/makesrna/intern/CMakeLists.txt index a6ed705ec67..0b43a5a6653 100644 --- a/source/blender/makesrna/intern/CMakeLists.txt +++ b/source/blender/makesrna/intern/CMakeLists.txt @@ -175,6 +175,7 @@ set(SRC ${DEFSRC} ${APISRC} ../../../../intern/clog/clog.c + ../../../../intern/guardedalloc/intern/leak_detector.cc ../../../../intern/guardedalloc/intern/mallocn.c ../../../../intern/guardedalloc/intern/mallocn_guarded_impl.c ../../../../intern/guardedalloc/intern/mallocn_lockfree_impl.c diff --git a/source/blender/makesrna/intern/makesrna.c b/source/blender/makesrna/intern/makesrna.c index b34c324bd91..045d098bf6a 100644 --- a/source/blender/makesrna/intern/makesrna.c +++ b/source/blender/makesrna/intern/makesrna.c @@ -5144,7 +5144,10 @@ static void mem_error_cb(const char *errorStr) int main(int argc, char **argv) { - int totblock, return_status = 0; + int return_status = 0; + + MEM_initialize_memleak_detection(); + MEM_set_error_callback(mem_error_cb); CLG_init(); @@ -5166,12 +5169,5 @@ int main(int argc, char **argv) CLG_exit(); - totblock = MEM_get_memory_blocks_in_use(); - if (totblock != 0) { - fprintf(stderr, "Error Totblock: %d\n", totblock); - MEM_set_error_callback(mem_error_cb); - MEM_printmemlist(); - } - return return_status; } diff --git a/source/blender/windowmanager/intern/wm_init_exit.c b/source/blender/windowmanager/intern/wm_init_exit.c index aa3f11c2515..945d5fd42e4 100644 --- a/source/blender/windowmanager/intern/wm_init_exit.c +++ b/source/blender/windowmanager/intern/wm_init_exit.c @@ -656,13 +656,6 @@ void WM_exit_ex(bContext *C, const bool do_python) BKE_blender_atexit(); - if (MEM_get_memory_blocks_in_use() != 0) { - size_t mem_in_use = MEM_get_memory_in_use() + MEM_get_memory_in_use(); - printf("Error: Not freed memory blocks: %u, total unfreed memory %f MB\n", - MEM_get_memory_blocks_in_use(), - (double)mem_in_use / 1024 / 1024); - MEM_printmemlist(); - } wm_autosave_delete(); BKE_tempdir_session_purge(); diff --git a/source/creator/creator.c b/source/creator/creator.c index abc443425a8..9268ed15923 100644 --- a/source/creator/creator.c +++ b/source/creator/creator.c @@ -295,6 +295,7 @@ int main(int argc, break; } } + MEM_initialize_memleak_detection(); } #ifdef BUILD_DATE -- cgit v1.2.3