I've run into a problem today that didn't dawn on me till I took a look at my call stack in Visual Studios. I'm currently trying to build a class that will display pieces of allocated memory that isn't cleaned up before the end of execution. The basic idea for now was to increment a counter within a singleton class whenever the new operator was used and decrement the counter once delete was called. I understand the logic error I created and I have created a work around. I'm currently looking for some advice for a cleaner, single instance solution then the one I have now. Here was my original code:

Code:
#ifndef CNEW_H
#define CNEW_H

#include "CAllocReporter.h"

// overload new
void* operator new(size_t size)
{
	void *p;

	p = malloc(size);

	if(!p) 
	{
		// Out of memory
		return NULL;
		/*bad_alloc ba;
		throw ba;*/
	}
	if(s_pAllocReporter)
		AllocReporter()->Add();

	return p;
}

#endif

class CAllocReporter
{
public:
	static CAllocReporter* Get()
	{
		if(m_pInstance)
			return m_pInstance;
		m_pInstance = new CAllocReporter();
	}

	void 

	void Add(){ m_AllocTotal++; }
	void Remove(){ m_AllocTotal--; }

	int GetAllocTotal(){ return m_AllocTotal; }
	
private:
	CAllocReporter(): m_AllocTotal(0)
	{}
	~CAllocReporter()
	{}

	static CAllocReporter* m_pInstance;

	int m_AllocTotal;
};

CAllocReporter* CAllocReporter::m_pInstance = NULL;

CAllocReporter* AllocReporter()
{
	return CAllocReporter::Get();
}
During the previous code why Singleton was never instantiated because the allocated memory was never returned from my new overload. In turn creating an infinite loop with the constant call to AllocReporter()->Add();

My work around:

Code:
void* operator new(size_t size)
{
	void *p;

	p = malloc(size);

	if(!p) 
	{
		// Out of memory
		return NULL;
		/*bad_alloc ba;
		throw ba;*/
	}
	if(s_pAllocReporter)
		s_pAllocReporter->Add();

	return p;
}

#ifndef CALLOC_H
#define CALLOC_H

#include <windows.h>
#include <iostream>
#include <cstdlib>

// not very happy with the implementation of this class, Singleton design will not
//		work with overloaded new because of the member call inside of it

class CAllocReporter
{
public:
	CAllocReporter(): m_AllocTotal(0)
	{}
	~CAllocReporter()
	{}

	void Add(){ m_AllocTotal++; }
	void Remove(){ m_AllocTotal--; }

	int GetAllocTotal(){ return m_AllocTotal; }
	
private:
	//static bool m_bInit;
	int			m_AllocTotal;
};

static CAllocReporter* s_pAllocReporter = NULL;

void Report()
{
	// we want to output this to our console
	if(s_pAllocReporter)
	{
		char msg[256];
		sprintf(msg, "Total memory allocated, including leaked: %d", s_pAllocReporter->GetAllocTotal());
		MessageBox( NULL, msg, "MsgBox", MB_OK | MB_ICONERROR );
	}
}

#endif
I'd like to find a better way to implement this idea ( single instance design mainly ).