This the code I'm having problems with. I took the code from an article over on flipcode (http://www.flipcode.com/tutorials/tut_memleak.shtml). I have placed the code in the stdafx.h file.
Code:
// Memory Debug code
#ifndef DEBUG_NEW
#ifdef _DEBUG
#define DEBUG_NEW new(__FILE__, __LINE__)
#else
#define DEBUG_NEW new
#endif
#define new DEBUG_NEW
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;
i = allocList->begin();
i == i;
if(!allocList)
{
return;
}
for(i = allocList->begin(); !(i == allocList->end()); i++)
{
if((*i)->address == addr)
{
allocList->remove((*i));
break;
}
}
};
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
typedef struct
{
DWORD address;
DWORD size;
char file[64];
DWORD line;
} ALLOC_INFO;
typedef list<ALLOC_INFO*> AllocList;
AllocList *allocList;
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);
};
Right now, when I place the "DumpUnfreed" function in my destructor for the main class, I get these errors:
Linking...
StdAfx.obj : error LNK2005: "void __cdecl DumpUnfreed(void)" (?DumpUnfreed@@YAXXZ) already defined in Game.obj
StdAfx.obj : error LNK2005: "class std::list<struct ALLOC_INFO *,class std::__default_alloc_template<1,0> > * allocList" (?allocList@@3PAV?$list@PAUALLOC_INFO@@V?$__defaul t_alloc_template@$00$0A@@std@@@std@@A) already defined in Game.obj
Debug/Text Scrabble.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.
Text Scrabble.exe - 3 error(s), 0 warning(s)
I understand that dumpunfreed is getting defined multiple times, but I'm unsure how to stop that from happening and actually have the code compile and link. Any help would be greatly appreciated!