Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Memory Leak Checker - "MemReport" by Chris Armitt
- //Programmer and Co-Founder of indie game dev team Molten Rock Studios
- /*
- Make sure you include this header file into every one of your header files and the file where your application
- entry point is. (i.e. void main, or int main)
- In your entry point, before anything else, have 'MRS_MemReport::GetInstance()',
- This will call the constructor of the singleton, setting up the logger.
- At the end of your entry point, put 'MRS_MemReport::GetInstance().Report()'
- and this will print all the memory leaks found within the program.
- Example:
- #include "MRS_MemReport.h"
- int main()
- {
- MRS_MemReport::GetInstance();
- //Application processing stuff
- MRS_MemReport::GetInstance().Report()
- return 0;
- }
- Once your application has ended, check the output window in your visual studio to see if you have any memory leaks,
- if you do, it will tell you where they are!
- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- !!!
- !!! Warning: It might return false positives on memory leaks if you have static classes or static based singletons
- !!! instiantate objects and rely on their destructors to clean up. To avoid this, add a shutdown function for each
- !!! of these classes and have the function contain the cleanup code. Make sure you call these shutdown functions
- !!! before you call 'MRS_MemReport::GetInstance().Report()'
- !!!
- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- */
- #ifdef _DEBUG
- #ifndef _MRS_MEMREPORT_H
- #define _MRS_MEMREPORT_H
- #include <map>
- #include <sstream>
- #include <Windows.h>
- #pragma warning(disable: 4291)
- struct MRS_AllocInfo
- {
- public:
- std::string filename;
- int line;
- MRS_AllocInfo(std::string fn, int ln)
- {
- filename = fn;
- line = ln;
- }
- };
- class MRS_MemReport
- {
- private:
- std::map<void *, MRS_AllocInfo> m_Leaks;
- MRS_MemReport();
- public:
- ~MRS_MemReport();
- static MRS_MemReport & GetInstance();
- static bool s_bLog;
- void LogNew(void* ptr, std::string file, int line);
- void LogDelete( void* ptr );
- void Report();
- };
- void * operator new( unsigned int size, const char *filename, int line );
- void operator delete( void *ptr );
- void operator delete[]( void *ptr );
- #undef new
- #define new new(__FILE__, __LINE__)
- #endif //#ifndef _MRS_MEMREPORT_H
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement