Click here to Skip to main content
15,895,777 members
Articles / Mobile Apps / Windows Mobile

Memory Leak Detector for C++

Rate me:
Please Sign up or sign in to vote.
3.24/5 (9 votes)
14 Dec 20064 min read 75.6K   1.6K   28  
Simple plug-in code for debugging memory leaks.
// DebugMemoryTest.cpp : Defines the entry point for the console application.
//

#ifdef WIN32
#include "stdafx.h"
#endif
#include <stdio.h>
#include "DebugMemory.h"

int __gxx_personality_v0;


//------------------------------------------------------------------------
// A simple base class used to show memory allocation
class CBaseClass
{
protected:
		int m_nX;
		int m_nY;
		char *m_ptr;

public:
		CBaseClass()
		{
				m_nX = 0;
				m_nY = 0;
				m_ptr = new char[8];				
		}
		CBaseClass(int nX, int nY)
		{
				m_nX = nX;
				m_nY = nY;
				m_ptr = new char[8];
		}
		~CBaseClass()
		{
				delete m_ptr;
		}
};

//------------------------------------------------------------------------
// A derived class to show inheritance and memory allocation
class CDerivedClass : public CBaseClass
{
protected:
		int *m_intArray;

public:
		CDerivedClass() : CBaseClass()
		{
				m_intArray = new int[32];
		}
		~CDerivedClass()
		{
				delete m_intArray;
		}
};


//------------------------------------------------------------------------
// Allocate memory using C runtime standard library routines
void AllocStdLibStyle(BOOL bFree)
{
	char *ptr = (char *)malloc(64);
	if (bFree)
			free(ptr);
	ptr = (char *)calloc(1,128);
	if (bFree)
			free(ptr);
}


//------------------------------------------------------------------------
// Allocate memory using C++ style memory interfaces
void AllocCPPStyle(BOOL bFree)
{
		// Pushing items onto the stack calls their constructors
		// which allocates memory in the constructors
		CBaseClass baseObjStack0;
		CBaseClass baseObjStack1(1,2);
		CDerivedClass derivedObjStack;

		// Allocating new objects allocates memory for the object
		// and calls the objects constructors which also allocate additional
		// memory for member variables.
		CBaseClass *pbaseObj0 = new CBaseClass(3,4);
		CBaseClass *pbaseObj1 = new CBaseClass();

		// Show how memory is allocated through inheritance
		CDerivedClass *pderivedObj0 = new CDerivedClass();

		// Allocate arrays of objects and show how the memory allocation
		// chain gets called.
		CBaseClass *pbaseArrayObj = new CBaseClass[32];
		CDerivedClass *pderivedArrayObj = new CDerivedClass[64];

		// Free all the memory we allocated if bFree is true
		if (bFree)
		{
				delete pbaseObj0;
				delete pbaseObj1;
				delete pderivedObj0;
				delete [] pbaseArrayObj;
				delete [] pderivedArrayObj;
		}
}


//------------------------------------------------------------------------
#ifdef WIN32
int _tmain(int argc, _TCHAR* argv[])
#else
int main()
#endif
{
	printf("\nPerforming Memory Test.");

	// Turn on the trace file 'dumpmemorylog.txt'
	DumpMemoryLogAllAllocations(TRUE);

	// Allocate then de-allocate memory
	AllocStdLibStyle(TRUE);
	AllocCPPStyle(TRUE);

	// Uncomment these next two lines if you want to see leaks in the memoryleak.txt file.
	//AllocStdLibStyle(FALSE);
	//AllocCPPStyle(FALSE);

	// Dump a report of any unfreed blocks to 'memoryleak.txt', then free the breadcrumb list
	DumpUnfreed(TRUE);

	printf("\nTest complete.  See memoryleak.txt and dumpmemorylog.txt for results.");
	printf("\nPress <CR>...");
	getchar();
	return 0;
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
John Klein is an experienced C/C++ and C# developer with an emphisis on WiFi and 802.11 systems. John has 19 years of development and management experience and participated in the development of the IEEE 802.11 wireless protocol.

John currently works at JiWire, Inc. as a principal engineer.

Comments and Discussions