1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 /** \file
18  * \ingroup MEM
19  */
20 
21 #include <cstdlib>
22 
23 #include "MEM_guardedalloc.h"
24 #include "mallocn_intern.h"
25 
26 bool leak_detector_has_run = false;
27 char free_after_leak_detection_message[] =
28     "Freeing memory after the leak detector has run. This can happen when using "
29     "static variables in C++ that are defined outside of functions. To fix this "
30     "error, use the 'construct on first use' idiom.";
31 
32 namespace {
33 
34 bool fail_on_memleak = false;
35 bool ignore_memleak = false;
36 
37 class MemLeakPrinter {
38  public:
~MemLeakPrinter()39   ~MemLeakPrinter()
40   {
41     if (ignore_memleak) {
42       return;
43     }
44     leak_detector_has_run = true;
45     const uint leaked_blocks = MEM_get_memory_blocks_in_use();
46     if (leaked_blocks == 0) {
47       return;
48     }
49     const size_t mem_in_use = MEM_get_memory_in_use();
50     printf("Error: Not freed memory blocks: %u, total unfreed memory %f MB\n",
51            leaked_blocks,
52            (double)mem_in_use / 1024 / 1024);
53     MEM_printmemlist();
54 
55     if (fail_on_memleak) {
56       /* There are many other ways to change the exit code to failure here:
57        * - Make the destructor noexcept(false) and throw an exception.
58        * - Call exit(EXIT_FAILURE).
59        * - Call terminate().
60        */
61       abort();
62     }
63   }
64 };
65 }  // namespace
66 
MEM_init_memleak_detection(void)67 void MEM_init_memleak_detection(void)
68 {
69   /**
70    * This variable is constructed when this function is first called. This should happen as soon as
71    * possible when the program starts.
72    *
73    * It is destructed when the program exits. During destruction, it will print information about
74    * leaked memory blocks. Static variables are destructed in reversed order of their
75    * construction. Therefore, all static variables that own memory have to be constructed after
76    * this function has been called.
77    */
78   static MemLeakPrinter printer;
79 }
80 
MEM_use_memleak_detection(bool enabled)81 void MEM_use_memleak_detection(bool enabled)
82 {
83   ignore_memleak = !enabled;
84 }
85 
MEM_enable_fail_on_memleak(void)86 void MEM_enable_fail_on_memleak(void)
87 {
88   fail_on_memleak = true;
89 }
90