HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_MemLeakDetector.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_MemLeakDetector.h
7  *
8  * COMMENTS: This file contains utilities to detect an isolate memory leaks.
9  * It currently only works on Windows debug builds.
10  * To enable the memory leak detector, make sure that
11  * UT_MEMLEAKDETECTOR_ENABLE is defined and recompile the .C file.
12  * Check the .C file for options to control the memory leak
13  * detection.
14  */
15 #ifndef __UT_MemLeakDetector_h__
16 #define __UT_MemLeakDetector_h__
17 
18 #include "UT_API.h"
19 #include <stdlib.h>
20 
21 // Only enable leak detection in Windows debug builds.
22 #if !defined(WIN32) || !defined(_DEBUG)
23 
24 #undef UT_MEMLEAKDETECTOR_ENABLE
25 
26 #else
27 
28 #define UT_MEMLEAKDETECTOR_ENABLE
29 
30 #endif
31 
32 
33 
34 
35 #ifndef UT_MEMLEAKDETECTOR_ENABLE
36 
37 inline void UTmemLeakDetectorReport() { }
39 inline void UTmemLeakDetectorCheckPoint() { }
40 
41 #else
42 
43 #ifndef UT_MEMLEAKDETCTOR_DISABLE_MAP_ALLOC
44 
45 // If .C files are compiled with this define, the reports generated here
46 // will be more useful.
47 #define _CRTDBG_MAP_ALLOC
48 
49 // The order of these includes is important.
50 #include <malloc.h>
51 #include <windows.h>
52 #include <crtdbg.h>
53 
54 #else
55 
56 #include <crtdbg.h>
57 
58 #endif // UT_MEMLEAKDETCTOR_DISABLE_MAP_ALLOC
59 
60 
61 /// Generates a report of the memory leaks encountered so far.
63 
64 /// Generates a report of all memory allocations since the last time
65 /// UTmemLeakDetectorCheckPoint() was called.
67 
68 /// Tags a spot in the execution of your program as a check point.
69 UT_API void UTmemLeakDetectorCheckpoint();
70 
71 #endif // UT_MEMLEAK_DETECTOR_ENABLE
72 
73 
74 namespace UT_MemLeakDetectorImpl
75 {
76 
78 {
79 public:
80  Block(
81  const char *file,
82  int line,
83  const char *label = nullptr,
84  bool dump_leaks = false);
85  ~Block();
86  void begin(
87  const char *file,
88  int line,
89  const char *label,
90  bool dump_leaks);
91  void end(bool dump_leaks = false);
92 private:
93 #ifdef UT_MEMLEAKDETECTOR_ENABLE
94  _CrtMemState myBeg;
95 #else
96  size_t myBeg;
97 #endif
98  bool myBegun:1;
99  bool myDumpLeaks:1;
100 };
101 
102 } // namespace UT_MemLeakDetectorDetail
103 
104 // UT_MEMLEAK_VAR(name) creates a unique variable name<X> where <X> is
105 // different everytime the macro is used.
106 #define UT_MEMLEAK_CONCAT_IMPL(x, y) x ## y
107 #define UT_MEMLEAK_CONCAT(x, y) UT_MEMLEAK_CONCAT_IMPL(x, y)
108 #define UT_MEMLEAK_VAR(name) UT_MEMLEAK_CONCAT(name, __COUNTER__)
109 
110 /// A memory block to track memory growth:
111 /// UT_MEMBLOCK(const char *label = nullptr, bool dump_leaks = false)
112 ///
113 /// On debug Windows builds, the dump_leaks option will also try to summarize
114 /// the outstanding memory allocations using the debug heap.
115 #define UT_MEMBLOCK(...) \
116  UT_MemLeakDetectorImpl::Block \
117  UT_MEMLEAK_VAR(memblock) (__FILE__, __LINE__, __VA_ARGS__) \
118  /**/
119 
120 /// Like UT_MEMBLOCK() but allows you to name the block variable
121 #define UT_MEMBLOCK_V(var, ...) \
122  UT_MemLeakDetectorImpl::Block var(__FILE__, __LINE__, __VA_ARGS__) \
123  /**/
124 
125 /// Used with UT_MEMBLOCK_V to do intermediate check points.
126 #define UT_MEMBLOCK_CHECK(old_var, new_var, ...) \
127  old_var.end(); \
128  UT_MemLeakDetectorImpl::Block new_var(__FILE__, __LINE__, __VA_ARGS__) \
129  /**/
130 
131 
132 #endif // __UT_MemLeakDetector_h__
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2544
#define UT_API
Definition: UT_API.h:12
GLuint GLuint end
Definition: glcorearb.h:474
void UTmemLeakDetectorCheckPoint()
void UTmemLeakDetectorReportAllocsSinceLastCheckpoint()
void UTmemLeakDetectorReport()