commonlib
memorywatch.h
Go to the documentation of this file.
1 
20 #pragma once
21 
22 #define _CRTDBG_MAP_ALLOC
23 
24 #include "memory.h"
25 #include <crtdbg.h>
26 #include <Windows.h>
27 #include <string>
28 
29 #ifdef _DEBUG
30  #define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
31  // Replace _NORMAL_BLOCK with _CLIENT_BLOCK if you want the
32  // allocations to be of _CLIENT_BLOCK type
33 #else
34  #define DBG_NEW new
35 #endif
36 
37 #ifdef DANGEROUS_NEW
38  #ifdef _DEBUG
39  // ReSharper disable once CppInconsistentNaming
40  #define new DBG_NEW
41  #endif
42 #endif
43 
44 BEGIN_NAMESPACE_MEMORY
45 
46 enum class MemoryWatchReport
47 {
48  file,
49  fileAndOutput,
50  fileAndWindow,
51  all
52 };
53 
58 {
59 public:
60  using CurrentMemoryState = _CrtMemState;
61 
68  static bool EnableMemoryLeakChecking(const std::string& reportFilePath = "",
69  MemoryWatchReport leakReport = MemoryWatchReport::fileAndOutput);
70 
75  static void SetMemoryCheckpoint(_CrtMemState* state);
76 
85  static int CompareMemoryCheckpoints(_CrtMemState* difference,
86  _CrtMemState const* oldState,
87  _CrtMemState const* newState);
88 
93  static void DumpMemoryCheckpointsLog(_CrtMemState const* state);
94 private:
95  static HANDLE CreateLogFile(const std::string& reportFilePath);
96  static void SetDumpMemoryLeaksFlag();
97 
98  static void SetLeakReportType(HANDLE hLogFile, MemoryWatchReport leakReport);
99  static void SetLeakReportDirection(int reportType, HANDLE hLogFile, int leakReport);
100 
101  static int ConvertReportTypeToInt(MemoryWatchReport leakReport);
102  static std::wstring ConvertToLPWSTR(const std::string& inputString);
103 };
104 
105 END_NAMESPACE_MEMORY
static void SetMemoryCheckpoint(_CrtMemState *state)
Function fills in the structure (_CrtMemState) with a snapshot of the current memory state...
static bool EnableMemoryLeakChecking(const std::string &reportFilePath="", MemoryWatchReport leakReport=MemoryWatchReport::fileAndOutput)
EnableMemoryLeakChecking Enables memory leak reporting in your application.
Finding Memory Leaks class using the CRT Library.
Definition: memorywatch.h:57
_CrtMemState CurrentMemoryState
Memory state structure at some key point (used for passing to _CrtMemCheckpoint)
Definition: memorywatch.h:60
static void DumpMemoryCheckpointsLog(_CrtMemState const *state)
DumpMemoryCheckpointsLog Outputs a dump of memory state.
static int CompareMemoryCheckpoints(_CrtMemState *difference, _CrtMemState const *oldState, _CrtMemState const *newState)
CompareMemoryCheckpoints compares the memory states oldState and newState and returns a result in the...