Advertisement
MaGuSware2012

MRS_MemReport.h

Feb 26th, 2014
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. //Memory Leak Checker - "MemReport" by Chris Armitt
  2. //Programmer and Co-Founder of indie game dev team Molten Rock Studios
  3. /*
  4.     Make sure you include this header file into every one of your header files and the file where your application
  5.     entry point is. (i.e. void main, or int main)
  6.  
  7.     In your entry point, before anything else, have 'MRS_MemReport::GetInstance()',
  8.     This will call the constructor of the singleton, setting up the logger.
  9.  
  10.     At the end of your entry point, put 'MRS_MemReport::GetInstance().Report()'
  11.     and this will print all the memory leaks found within the program.
  12.  
  13.     Example:
  14.  
  15.     #include "MRS_MemReport.h"
  16.     int main()
  17.     {
  18.         MRS_MemReport::GetInstance();
  19.  
  20.         //Application processing stuff
  21.  
  22.         MRS_MemReport::GetInstance().Report()
  23.         return 0;
  24.     }
  25.  
  26.     Once your application has ended, check the output window in your visual studio to see if you have any memory leaks,
  27.     if you do, it will tell you where they are!
  28.  
  29.     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  30.     !!!
  31.     !!!     Warning: It might return false positives on memory leaks if you have static classes or static based singletons
  32.     !!!     instiantate objects and rely on their destructors to clean up. To avoid this, add a shutdown function for each
  33.     !!!     of these classes and have the function contain the cleanup code. Make sure you call these shutdown functions
  34.     !!!     before you call 'MRS_MemReport::GetInstance().Report()'
  35.     !!!
  36.     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  37. */
  38.  
  39. #ifdef _DEBUG
  40.     #ifndef _MRS_MEMREPORT_H
  41.  
  42.         #define _MRS_MEMREPORT_H
  43.  
  44.         #include <map>
  45.         #include <sstream>
  46.         #include <Windows.h>
  47.  
  48.         #pragma warning(disable: 4291)
  49.  
  50.         struct MRS_AllocInfo
  51.         {
  52.         public:
  53.             std::string     filename;
  54.             int             line;
  55.  
  56.             MRS_AllocInfo(std::string fn, int ln)
  57.             {
  58.                 filename    = fn;
  59.                 line        = ln;
  60.             }
  61.         };
  62.  
  63.         class MRS_MemReport
  64.         {
  65.         private:
  66.             std::map<void *, MRS_AllocInfo> m_Leaks;
  67.             MRS_MemReport();
  68.  
  69.         public:
  70.             ~MRS_MemReport();
  71.             static MRS_MemReport & GetInstance();
  72.             static bool s_bLog;
  73.  
  74.             void LogNew(void* ptr, std::string file, int line);
  75.             void LogDelete( void* ptr );
  76.  
  77.             void Report();
  78.         };
  79.  
  80.         void * operator new( unsigned int size, const char *filename, int line );
  81.         void   operator delete( void *ptr );
  82.         void   operator delete[]( void *ptr );
  83.  
  84.         #undef new
  85.         #define new new(__FILE__, __LINE__)
  86.  
  87.     #endif //#ifndef _MRS_MEMREPORT_H
  88. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement