[ create a new paste ] login | about

Link: http://codepad.org/SZNmwp6S    [ raw code | output | fork ]

C++, pasted on May 26:
#include "leakdetection.h"
#include <stdio.h>
#include <iostream>
#include <list>
using namespace std;

void AddTrack(DWORD addr,  DWORD asize,  const char *fname, DWORD lnum);
void RemoveTrack(DWORD addr);

typedef struct {
	      DWORD	address;
	      DWORD	size;
	      char	file[255];
	      DWORD	line;
      } ALLOC_INFO;

typedef list<ALLOC_INFO*> AllocList;

AllocList *allocList;

void AddTrack(DWORD addr,  DWORD asize,  const char *fname, DWORD lnum)
{
	ALLOC_INFO *info;

	if(!allocList) {
		allocList = new(AllocList);
	}

	info = new(ALLOC_INFO);
	info->address = addr;
	strncpy(info->file, fname, 255);
	info->line = lnum;
	info->size = asize;
	allocList->insert(allocList->begin(), info);
};

void RemoveTrack(DWORD addr)
{
	AllocList::iterator i;

	if(!allocList)
		return;
	for(i = allocList->begin(); i != allocList->end(); i++)
	{
		if((*i)->address == addr)
		{
			allocList->remove((*i));
			break;
		}
	}
};

void DumpUnfreed()
{
	AllocList::iterator i;
	DWORD totalSize = 0;
	char buf[1024];

	if(!allocList)
		return;

	for(i = allocList->begin(); i != allocList->end(); i++) {
		sprintf(buf, "%-20s:\tLINE %d,\tADDRESS %d\t%d unfreed\n",
			(*i)->file, (*i)->line, (*i)->address, (*i)->size);
		cout << buf;
		totalSize += (*i)->size;
	}
	sprintf(buf, "---------------------------------------------\n");
	cout << buf;
	sprintf(buf, "Total Unfreed: %d bytes\n", totalSize);
	cout << buf;
};

inline void *  operator new(DWORD size,
								   const char *file, int line)
{
	void *ptr = (void *)malloc(size);
	AddTrack((DWORD)ptr, size, file, line);
	return(ptr);
};

void operator delete (void *p) throw()
{
	RemoveTrack((DWORD)p);
	free(p);
};


// Overload array new
void* operator new[](DWORD siz, const char* file, long line) {
	return operator new(siz, file, line);
}

// Override scalar delete
//void operator delete (void *p)
//{
// free(p);
//}
//void operator delete(void* p) {
//	RemoveTrack((DWORD)p);
//	free(p);
//}

// Override array delete
void operator delete[](void* p) throw() {
operator delete(p);
} ///:~

//#define DEBUG_NEW new(__FILE__, __LINE__)
//#define new DEBUG_NEW


Output:
1
2
3
Line 26: error: leakdetection.h: No such file or directory
Line 7: error: variable or field 'AddTrack' declared void
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: