HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 "UT_NonCopyable.h"
20 #include <stdlib.h>
21 
22 // Only enable leak detection in Windows debug builds.
23 #if !defined(WIN32) || !defined(_DEBUG)
24 
25 #undef UT_MEMLEAKDETECTOR_ENABLE
26 
27 #else
28 
29 #define UT_MEMLEAKDETECTOR_ENABLE
30 
31 #endif
32 
33 
34 
35 
36 #ifndef UT_MEMLEAKDETECTOR_ENABLE
37 
38 inline void UTmemLeakDetectorReport() { }
40 inline void UTmemLeakDetectorCheckPoint() { }
41 
42 #else
43 
44 #ifndef UT_MEMLEAKDETCTOR_DISABLE_MAP_ALLOC
45 
46 // If .C files are compiled with this define, the reports generated here
47 // will be more useful.
48 #define _CRTDBG_MAP_ALLOC
49 
50 // The order of these includes is important.
51 #include <malloc.h>
52 #include <windows.h>
53 #include <crtdbg.h>
54 
55 #else
56 
57 #include <crtdbg.h>
58 
59 #endif // UT_MEMLEAKDETCTOR_DISABLE_MAP_ALLOC
60 
61 
62 /// Generates a report of the memory leaks encountered so far.
64 
65 /// Generates a report of all memory allocations since the last time
66 /// UTmemLeakDetectorCheckPoint() was called.
68 
69 /// Tags a spot in the execution of your program as a check point.
70 UT_API void UTmemLeakDetectorCheckpoint();
71 
72 #endif // UT_MEMLEAK_DETECTOR_ENABLE
73 
74 
75 namespace UT_MemLeakDetectorImpl
76 {
77 
79 {
80 public:
81  Block(
82  const char *file,
83  int line,
84  const char *label = nullptr,
85  bool dump_leaks = false);
86  ~Block();
87 
89 
90  void begin(
91  const char *file,
92  int line,
93  const char *label,
94  bool dump_leaks);
95  void end(bool dump_leaks = false);
96 private:
97 #ifdef UT_MEMLEAKDETECTOR_ENABLE
98  _CrtMemState myBeg;
99 #else
100  size_t myBeg;
101 #endif
102  bool myBegun:1;
103  bool myDumpLeaks:1;
104 };
105 
106 } // namespace UT_MemLeakDetectorDetail
107 
108 // UT_MEMLEAK_VAR(name) creates a unique variable name<X> where <X> is
109 // different everytime the macro is used.
110 #define UT_MEMLEAK_CONCAT_IMPL(x, y) x ## y
111 #define UT_MEMLEAK_CONCAT(x, y) UT_MEMLEAK_CONCAT_IMPL(x, y)
112 #define UT_MEMLEAK_VAR(name) UT_MEMLEAK_CONCAT(name, __COUNTER__)
113 
114 /// A memory block to track memory growth:
115 /// UT_MEMBLOCK(const char *label = nullptr, bool dump_leaks = false)
116 ///
117 /// On debug Windows builds, the dump_leaks option will also try to summarize
118 /// the outstanding memory allocations using the debug heap.
119 #define UT_MEMBLOCK(...) \
120  UT_MemLeakDetectorImpl::Block \
121  UT_MEMLEAK_VAR(memblock) (__FILE__, __LINE__, __VA_ARGS__) \
122  /**/
123 
124 /// Like UT_MEMBLOCK() but allows you to name the block variable
125 #define UT_MEMBLOCK_V(var, ...) \
126  UT_MemLeakDetectorImpl::Block var(__FILE__, __LINE__, __VA_ARGS__) \
127  /**/
128 
129 /// Used with UT_MEMBLOCK_V to do intermediate check points.
130 #define UT_MEMBLOCK_CHECK(old_var, new_var, ...) \
131  old_var.end(); \
132  UT_MemLeakDetectorImpl::Block new_var(__FILE__, __LINE__, __VA_ARGS__) \
133  /**/
134 
135 
136 #endif // __UT_MemLeakDetector_h__
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2545
#define UT_API
Definition: UT_API.h:14
GLuint GLuint end
Definition: glcorearb.h:475
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
void UTmemLeakDetectorCheckPoint()
void UTmemLeakDetectorReportAllocsSinceLastCheckpoint()
void UTmemLeakDetectorReport()
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:483