   Memory Allocation checking code
  There was an article way back in 2000 over on flipcode about adding some code to check for memory leaks. I added the code, like so:File: StdAfx.h:#include <iostream.h>#include <stdlib.h>#include <mmsystem.h>#include <stl/list.h>// Memory Debug code#ifdef _DEBUG#define DEBUG_NEW new(__FILE__, __LINE__)#else#define DEBUG_NEW new#endif#define new DEBUG_NEW#ifdef _DEBUGtypedef struct {    DWORDaddress;    DWORDsize;    charfile[64];    DWORDline;} ALLOC_INFO;typedef list 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);};#endif----------------------------------------------------------------and I get these errors when I compile:Compiling...StdAfx.cppd:\projects\text scrabble\stdafx.h(32) : warning C4005: 'DEBUG_NEW' : macroredefinition   c:\program files\microsoft visualstudio\vc98\mfc\include\afx.h(1632) : see previous definition of 'DEBUG_NEW'd:\projects\text scrabble\stdafx.h(78) : error C2667: '!=' : none of 2overload have a best conversiond:\projects\text scrabble\stdafx.h(78) : error C2593: 'operator !=' isambiguousd:\projects\text scrabble\stdafx.h(100) : error C2667: '!=' : none of 2overload have a best conversiond:\projects\text scrabble\stdafx.h(100) : error C2593: 'operator !=' isambiguousd:\projects\text scrabble\stdafx.h(114) : error C2059: syntax error :'string'd:\projects\text scrabble\stdafx.h(116) : error C2091: function returnsfunctiond:\projects\text scrabble\stdafx.h(116) : error C2809: 'operator new' has noformal parametersd:\projects\text scrabble\stdafx.h(117) : error C2065: 'size' : undeclaredidentifierd:\projects\text scrabble\stdafx.h(118) : error C2065: 'file' : undeclaredidentifierd:\projects\text scrabble\stdafx.h(118) : error C2065: 'line' : undeclaredidentifierError executing cl.exe.I'm quite confused why it doesn't let you compare 2 pointers. I assume thatall the = operator does is check memory addresses? And the "syntax error:'string'". I'm totally confused, since "string" doesn't appear on that lineat all, nor anywhere else. I am running VS6 sp5. Any help would be greatlyappreciated![edited by - Lister on August 19, 2003 12:51:32 PM]
  Ohhh... at first look:#ifdef _DEBUG#define DEBUG_NEW new(__FILE__, __LINE__)#else#define DEBUG_NEW new#endif#define new DEBUG_NEWtry add #ifndef DEBUG_NEW like this #ifndef DEBUG_NEW#ifdef _DEBUG#define DEBUG_NEW new(__FILE__, __LINE__)#else#define DEBUG_NEW new#endif#define new DEBUG_NEW#endifshould help  "The Gods Made Heavy Metal And They Saw That It Was GoodThey Said To Play It Louder Than Hell We Promised That We WouldWhen Losers Say Its Over With You Know That It’s A LieThe Gods Made Heavy Metal And It’s Never Gonna Die"THE GODS MADE HEAVY METAL/by  ManOwaR  ;  That fixed all but 4 errors, Thank you!!!...so I guess that since every file includes that .h file, DEBUG_NEW was getting defined multiple times, and messing things up?Here's what's left....d:\projects\text scrabble\stdafx.h(79) : error C2667: '!=' : none of 2 overload have a best conversiond:\projects\text scrabble\stdafx.h(79) : error C2593: 'operator !=' is ambiguousd:\projects\text scrabble\stdafx.h(101) : error C2667: '!=' : none of 2 overload have a best conversiond:\projects\text scrabble\stdafx.h(101) : error C2593: 'operator !=' is ambiguousSo there isn't a '!=' operator for iterators?? Why would he put that code in an article then?   [edited by - Lister on August 19, 2003 4:24:56 PM] [edited by - Lister on August 19, 2003 4:25:15 PM];  There is definetely a ''!='' operator for iterators...so why is it ambiguous? I don''t really use the STL, so I''m kinda at a loss here 