Original Post
Hey everyone, I'm having some trouble with the memory leak detector described in this tutorial. It usually acts fine for smaller projects, but as this codebase grows, i run into more and more problems. The problem i get is "Unhandled exception at 0x0049706b in HorrorGame.exe: 0xC0000005: Access violation reading location 0x00000004" that points to the code in xtree, which means there's a problem in the stl map that i'm using. I can pinpoint when it happens: destruction of he memory detector class, which is a singleton. I can also know which line I can comment/uncomment to make it work, but I can't figure out why it's happening. Here's the code: MemTracker.h: MemTracker.cpp: the line that can be uncommented for the program not to crash is:
_Nodeptr _Ubound(const key_type& _Keyval) const
{ // find leftmost node greater than _Keyval
>>>>>>>>>> _Nodeptr _Pnode = _Root();
_Nodeptr _Wherenode = _Myhead; // end() if search fails
...
#ifndef _MemTracker_h
#define _MemTracker_h
#include <map>
#include <string>
#include <assert.h>
class CMemTracker
{
public:
static CMemTracker * GetInstance ( );
static void Destroy ( );
void AllocNew ( unsigned long address, const char * file, int line );
void DeleteOld( unsigned long address );
typedef std::map< unsigned long, std::string >::iterator MemIter;
std::map< unsigned long, std::string > memAllocs;
private:
static CMemTracker * _instance;
};
#ifndef _NDEBUG
inline void * __cdecl operator new(unsigned int size, const char *file, int line)
{
void * ptr = ( void* )malloc( size );
if( CMemTracker::GetInstance() )
CMemTracker::GetInstance()->AllocNew( (unsigned long)ptr, file, line );
return ptr;
};
inline void __cdecl operator delete ( void *p )
{
if( CMemTracker::GetInstance() )
CMemTracker::GetInstance()->DeleteOld( (unsigned long)p );
free( p );
};
inline void * __cdecl operator new[]( unsigned int size, const char *file, int line )
{
void * ptr = ( void* )malloc( size );
if( CMemTracker::GetInstance() )
CMemTracker::GetInstance()->AllocNew( (unsigned long)ptr, file, line );
return ptr;
};
inline void __cdecl operator delete []( void *p )
{
if( CMemTracker::GetInstance() )
CMemTracker::GetInstance()->DeleteOld( (unsigned long)p );
free( p );
};
#define DEBUG_NEW new(__FILE__, __LINE__)
#else
#define DEBUG_NEW new
#endif
#endif
#include "MemTracker.h"
CMemTracker * CMemTracker::_instance;
CMemTracker * CMemTracker::GetInstance ( )
{
if( !_instance )
{
_instance = new CMemTracker ( );
}
return _instance;
}
void CMemTracker::Destroy ( )
{
if( _instance )
{
delete _instance;
_instance = 0;
}
}
void CMemTracker::AllocNew ( unsigned long address, const char * file, int line )
{
char temp [ 16 ];
std::string str;
str += file;
str += ": ";
itoa( line, temp, 10 );
str += temp;
memAllocs[ address ] = str;
}
void CMemTracker::DeleteOld( unsigned long address )
{
memAllocs.erase( address );
}
memAllocs.erase( address );
the actual code i stepped into right before i got the error is:
delete _instance;
I originally thought it was a problem where the class was destructed but DeleteOld was called afterward, but I have a check to make sure the pointer is valid first. Any thoughts?