Hi!
I try to follow this article: http://flipcode.com/archives/How%5FTo%5FFind%5FMemory%5FLeaks.shtml
to overload my new and delete functions in order to track memory leaks.
however - if i try to compile, I get a
C2365: "operator new": redefinition; previous definition was a "function"
in the file xdebug
xdebug gets included in xlocale - however, i can't find where my project is including xlocale
I'm using MFC for multithreading in my project.
Can someone tell me how i can get my memory leak tracking to work?
Thanks!
//edit: So this is my findMemoryLeak.h which i include in the end of stdafx.h
#ifndef _FINDMEMORYLEAK_H
#define _FINDMEMORYLEAK_H
#include <list>
using namespace std;
#ifdef _DEBUG
typedef struct {
DWORD address;
DWORD size;
char file[64];
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, 63);
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, "%-50s:\t\tLINE %d,\t\tADDRESS %d\t%d unfreed\n",
(*i)->file, (*i)->line, (*i)->address, (*i)->size);
OutputDebugString(buf);
totalSize += (*i)->size;
}
sprintf(buf, "-----------------------------------------------------------\n");
OutputDebugString(buf);
sprintf(buf, "Total Unfreed: %d bytes\n", totalSize);
OutputDebugString(buf);
};
inline void * __cdecl operator new(unsigned int size, const char *file, int line)
{
void *ptr = (void *)malloc(size);
AddTrack((DWORD)ptr, size, file, line);
return(ptr);
};
inline void __cdecl operator delete(void *p)
{
RemoveTrack((DWORD)p);
free(p);
};
inline void * __cdecl operator new[](unsigned int size, const char *file, int line)
{
void *ptr = (void *)malloc(size);
AddTrack((DWORD)ptr, size, file, line);
return(ptr);
};
inline void __cdecl operator delete[](void *p)
{
RemoveTrack((DWORD)p);
free(p);
};
#endif
//make the normal new function call the new function with three parameters
#ifdef _DEBUG
#define DEBUG_NEW new(__FILE__, __LINE__)
#else
#define DEBUG_NEW new
#endif
#define new DEBUG_NEW
#endif
when I include it like this in the end of stdafx.h, i get thousands of compilererrors, most of them in either xdebug or xlocale, with the first being
C2365: "operator new": redefinition; previous definition was a "function"
in xdebug on line 32