Click here to Skip to main content
15,895,774 members
Articles / Programming Languages / C++

Memory Leak Detection

Rate me:
Please Sign up or sign in to vote.
4.71/5 (55 votes)
3 Jul 2009CPOL7 min read 481.4K   14.8K   217  
Adding Memory Leak Detection in your applications
/*************************************************************
 Author		: David A. Jones
 File Name	: MemLeakDetect.h
 Date		: July 30, 2004
 Synopsis	:		 
			A trace memory feature for source code to trace and
			find memory related bugs. 

 Future		:
				1) Memory corruption
				2) Freeing memory without allocating
				3) Freeing memory twice
				4) Not Freeing memory at all
				5) over running memory boundardies


****************************************************************/
#define _CRTDBG_MAP_ALLOC

#define _CRTBLD
#include <afxtempl.h>
#include "dbgint.h"
#include <ImageHlp.h>

#pragma comment( lib, "imagehlp.lib" )

#define MLD_MAX_NAME_LENGTH			256
#define MLD_MAX_TRACEINFO			256

typedef struct _STACKFRAMEENTRY
{
		ADDRESS   AddrPC;
		ADDRESS   AddrFrame;
	
} STACKFRAMEENTRY;

class CMemLeakDetect
{
	public:
		class AllocBlockInfo
		{
			public:
				inline AllocBlockInfo() {};
				inline ~AllocBlockInfo() {};
				inline AllocBlockInfo(AllocBlockInfo& abi)
				{
					address		= abi.address;
					size		= abi.size;
					lineNumber	= abi.lineNumber;
					occurance	= abi.occurance;
					memcpy(&traceinfo[0], &abi.traceinfo[0], sizeof(traceinfo));
					memcpy(fileName, abi.fileName, sizeof(fileName));
				};

			void*				address;
			DWORD				size;
			char				fileName[MLD_MAX_NAME_LENGTH];
			DWORD				lineNumber;
			DWORD				occurance;
			STACKFRAMEENTRY		traceinfo[MLD_MAX_TRACEINFO];
		};

	public:
		CMemLeakDetect();
		~CMemLeakDetect();
		void Init();
		void End();
		void addMemoryTrace(void* addr,  DWORD asize,  char *fname, DWORD lnum);
		void redoMemoryTrace(void* addr,  void* oldaddr, DWORD asize,  char *fname, DWORD lnum);
		void removeMemoryTrace(void* addr);
		void cleanupMemoryTrace();
		void dumpMemoryTrace();
		//

	CMap<LPVOID, LPVOID, AllocBlockInfo, AllocBlockInfo> m_AllocatedMemoryList;
	DWORD memoccurance;
	bool  isLocked;
	//
	private:

		BOOL initSymInfo(char* lpUserPath);
		BOOL cleanupSymInfo();
		void symbolPaths( char* lpszSymbolPaths);
		void symStackTrace(STACKFRAMEENTRY* pStacktrace);
		BOOL symFunctionInfoFromAddresses(ULONG fnAddress, ULONG stackAddress, char* lpszSymbol);
		BOOL symSourceInfoFromAddress(UINT address, char* lpszSourceInfo);
		BOOL symModuleNameFromAddress(UINT address, char* lpszModule);

		HANDLE				m_hProcess;
		PIMAGEHLP_SYMBOL	m_pSymbol;
		DWORD				m_dwsymBufSize;
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions