Blender  V3.3
leak_detector.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include <cstdio> /* Needed for `printf` on WIN32/APPLE. */
8 #include <cstdlib>
9 
10 #include "MEM_guardedalloc.h"
11 #include "mallocn_intern.h"
12 
13 bool leak_detector_has_run = false;
15  "Freeing memory after the leak detector has run. This can happen when using "
16  "static variables in C++ that are defined outside of functions. To fix this "
17  "error, use the 'construct on first use' idiom.";
18 
19 namespace {
20 
21 bool fail_on_memleak = false;
22 bool ignore_memleak = false;
23 
24 class MemLeakPrinter {
25  public:
26  ~MemLeakPrinter()
27  {
28  if (ignore_memleak) {
29  return;
30  }
31  leak_detector_has_run = true;
32  const uint leaked_blocks = MEM_get_memory_blocks_in_use();
33  if (leaked_blocks == 0) {
34  return;
35  }
36  const size_t mem_in_use = MEM_get_memory_in_use();
37  printf("Error: Not freed memory blocks: %u, total unfreed memory %f MB\n",
38  leaked_blocks,
39  (double)mem_in_use / 1024 / 1024);
41 
42  if (fail_on_memleak) {
43  /* There are many other ways to change the exit code to failure here:
44  * - Make the destructor noexcept(false) and throw an exception.
45  * - Call exit(EXIT_FAILURE).
46  * - Call terminate().
47  */
48  abort();
49  }
50  }
51 };
52 } // namespace
53 
55 {
65  static MemLeakPrinter printer;
66 }
67 
69 {
70  ignore_memleak = !enabled;
71 }
72 
74 {
75  fail_on_memleak = true;
76 }
unsigned int uint
Definition: BLI_sys_types.h:67
Read Guarded memory(de)allocation.
bool enabled
void MEM_use_memleak_detection(bool enabled)
void MEM_enable_fail_on_memleak()
bool leak_detector_has_run
void MEM_init_memleak_detection()
char free_after_leak_detection_message[]
size_t(* MEM_get_memory_in_use)(void)
Definition: mallocn.c:45
unsigned int(* MEM_get_memory_blocks_in_use)(void)
Definition: mallocn.c:46
void(* MEM_printmemlist)(void)
Definition: mallocn.c:39
static size_t mem_in_use