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 --- intern/guardedalloc/CMakeLists.txt | 1 + intern/guardedalloc/MEM_guardedalloc.h | 4 ++ intern/guardedalloc/intern/leak_detector.cc | 61 ++++++++++++++++++++++ intern/guardedalloc/intern/mallocn_guarded_impl.c | 4 ++ intern/guardedalloc/intern/mallocn_inline.h | 8 +++ intern/guardedalloc/intern/mallocn_intern.h | 11 ++++ intern/guardedalloc/intern/mallocn_lockfree_impl.c | 4 ++ 7 files changed, 93 insertions(+) create mode 100644 intern/guardedalloc/intern/leak_detector.cc (limited to 'intern') diff --git a/intern/guardedalloc/CMakeLists.txt b/intern/guardedalloc/CMakeLists.txt index cb24df65ba0..1ab365a376a 100644 --- a/intern/guardedalloc/CMakeLists.txt +++ b/intern/guardedalloc/CMakeLists.txt @@ -28,6 +28,7 @@ set(INC_SYS ) set(SRC + ./intern/leak_detector.cc ./intern/mallocn.c ./intern/mallocn_guarded_impl.c ./intern/mallocn_lockfree_impl.c diff --git a/intern/guardedalloc/MEM_guardedalloc.h b/intern/guardedalloc/MEM_guardedalloc.h index 1318aa10697..604330bd1d3 100644 --- a/intern/guardedalloc/MEM_guardedalloc.h +++ b/intern/guardedalloc/MEM_guardedalloc.h @@ -211,6 +211,10 @@ extern size_t (*MEM_get_peak_memory)(void) ATTR_WARN_UNUSED_RESULT; extern const char *(*MEM_name_ptr)(void *vmemh); #endif +/** This should be called as early as possible in the program. When it has been called, information + * about memory leaks will be printed on exit. */ +void MEM_initialize_memleak_detection(void); + /* Switch allocator to slower but fully guarded mode. */ void MEM_use_guarded_allocator(void); diff --git a/intern/guardedalloc/intern/leak_detector.cc b/intern/guardedalloc/intern/leak_detector.cc new file mode 100644 index 00000000000..4b2689ee28c --- /dev/null +++ b/intern/guardedalloc/intern/leak_detector.cc @@ -0,0 +1,61 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +/** \file + * \ingroup MEM + */ + +#include "MEM_guardedalloc.h" +#include "mallocn_intern.h" + +bool leak_detector_has_run = false; +char free_after_leak_detection_message[] = + "Freeing memory after the leak detector has run. This can happen when using " + "static variables in C++ that are defined outside of functions. To fix this " + "error, use the 'construct on first use' idiom."; + +namespace { +class MemLeakPrinter { + public: + ~MemLeakPrinter() + { + leak_detector_has_run = true; + const uint leaked_blocks = MEM_get_memory_blocks_in_use(); + if (leaked_blocks == 0) { + return; + } + const size_t mem_in_use = MEM_get_memory_in_use(); + printf("Error: Not freed memory blocks: %u, total unfreed memory %f MB\n", + leaked_blocks, + (double)mem_in_use / 1024 / 1024); + MEM_printmemlist(); + } +}; +} // namespace + +void MEM_initialize_memleak_detection(void) +{ + /** + * This variable is constructed when this function is first called. This should happen as soon as + * possible when the program starts. + * + * It is destructed when the program exits. During destruction, it will print information about + * leaked memory blocks. Static variables are destructed in reversed order of their + * construction. Therefore, all static variables that own memory have to be constructed after + * this function has been called. + */ + static MemLeakPrinter printer; +} diff --git a/intern/guardedalloc/intern/mallocn_guarded_impl.c b/intern/guardedalloc/intern/mallocn_guarded_impl.c index 5e523204020..2c207935e43 100644 --- a/intern/guardedalloc/intern/mallocn_guarded_impl.c +++ b/intern/guardedalloc/intern/mallocn_guarded_impl.c @@ -898,6 +898,10 @@ void MEM_guarded_freeN(void *vmemh) memt = (MemTail *)(((char *)memh) + sizeof(MemHead) + memh->len); if (memt->tag3 == MEMTAG3) { + if (leak_detector_has_run) { + MemorY_ErroR(memh->name, free_after_leak_detection_message); + } + memh->tag1 = MEMFREE; memh->tag2 = MEMFREE; memt->tag3 = MEMFREE; diff --git a/intern/guardedalloc/intern/mallocn_inline.h b/intern/guardedalloc/intern/mallocn_inline.h index f8bb7861fc9..4e73eb9bad6 100644 --- a/intern/guardedalloc/intern/mallocn_inline.h +++ b/intern/guardedalloc/intern/mallocn_inline.h @@ -33,6 +33,10 @@ #ifndef __MALLOCN_INLINE_H__ #define __MALLOCN_INLINE_H__ +#ifdef __cplusplus +extern "C" { +#endif + MEM_INLINE bool MEM_size_safe_multiply(size_t a, size_t b, size_t *result) { /* A size_t with its high-half bits all set to 1. */ @@ -52,4 +56,8 @@ MEM_INLINE bool MEM_size_safe_multiply(size_t a, size_t b, size_t *result) return ((high_bits & (a | b)) == 0 || (*result / b == a)); } +#ifdef __cplusplus +} +#endif + #endif /* __MALLOCN_INLINE_H__ */ diff --git a/intern/guardedalloc/intern/mallocn_intern.h b/intern/guardedalloc/intern/mallocn_intern.h index ef8845a66b3..8fc3e432157 100644 --- a/intern/guardedalloc/intern/mallocn_intern.h +++ b/intern/guardedalloc/intern/mallocn_intern.h @@ -100,11 +100,18 @@ size_t malloc_usable_size(void *ptr); #include "mallocn_inline.h" +#ifdef __cplusplus +extern "C" { +#endif + #define ALIGNED_MALLOC_MINIMUM_ALIGNMENT sizeof(void *) void *aligned_malloc(size_t size, size_t alignment); void aligned_free(void *ptr); +extern bool leak_detector_has_run; +extern char free_after_leak_detection_message[]; + /* Prototypes for counted allocator functions */ size_t MEM_lockfree_allocN_len(const void *vmemh) ATTR_WARN_UNUSED_RESULT; void MEM_lockfree_freeN(void *vmemh); @@ -191,4 +198,8 @@ size_t MEM_guarded_get_peak_memory(void) ATTR_WARN_UNUSED_RESULT; const char *MEM_guarded_name_ptr(void *vmemh); #endif +#ifdef __cplusplus +} +#endif + #endif /* __MALLOCN_INTERN_H__ */ diff --git a/intern/guardedalloc/intern/mallocn_lockfree_impl.c b/intern/guardedalloc/intern/mallocn_lockfree_impl.c index 205cc688d72..282c852fedd 100644 --- a/intern/guardedalloc/intern/mallocn_lockfree_impl.c +++ b/intern/guardedalloc/intern/mallocn_lockfree_impl.c @@ -101,6 +101,10 @@ size_t MEM_lockfree_allocN_len(const void *vmemh) void MEM_lockfree_freeN(void *vmemh) { + if (leak_detector_has_run) { + print_error(free_after_leak_detection_message); + } + MemHead *memh = MEMHEAD_FROM_PTR(vmemh); size_t len = MEM_lockfree_allocN_len(vmemh); -- cgit v1.2.3