TuttleOFX
1
|
00001 #ifndef _WINDOWSMEMORYLEAKS_HPP_ 00002 #define _WINDOWSMEMORYLEAKS_HPP_ 00003 00004 #ifdef __WINDOWS__ 00005 00006 #include <tuttle/common/patterns/StaticSingleton.hpp> 00007 #include <crtdbg.h> 00008 00009 /** 00010 * @brief Class to detect memory leaks using the _CrtSetDbgFlag function (windows only). 00011 * 00012 * It's provided by the windows API, it's easy to use and very safe. 00013 * You just need to call CrtSetDbgFlag anywhere in your code ! 00014 * Here is a simple: 00015 * 00016 * #include <crtdbg.h> 00017 * 00018 * #ifdef _DEBUG 00019 * #define new new(_NORMAL_BLOCK, THIS_FILE, __LINE__) 00020 * #undef THIS_FILE 00021 * static char THIS_FILE[] = __FILE__; 00022 * #endif 00023 * 00024 * void main() 00025 * { 00026 * _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); 00027 * 00028 * char* memory_leak = new char[256]; 00029 * } 00030 * 00031 * And that's all ! 00032 * 00033 * If your program has memory leaks, you'll be informed at the end of the program execution. 00034 */ 00035 class MemoryLeaks : public StaticSingleton<MemoryLeaks> 00036 { 00037 public: 00038 friend class StaticSingleton<MemoryLeaks>; 00039 00040 private: 00041 _CrtMemState m_checkpoint; 00042 00043 protected: 00044 MemoryLeaks() 00045 { 00046 _CrtMemCheckpoint( &m_checkpoint ); 00047 } 00048 00049 ~MemoryLeaks() 00050 { 00051 _CrtMemState checkpoint; 00052 00053 _CrtMemCheckpoint( &checkpoint ); 00054 00055 _CrtMemState diff; 00056 _CrtMemDifference( &diff, &m_checkpoint, &checkpoint ); 00057 00058 _CrtMemDumpStatistics( &diff ); 00059 _CrtMemDumpAllObjectsSince( &diff ); 00060 } 00061 00062 }; 00063 00064 #endif 00065 00066 #endif 00067