Tracking Memory Leaks
I recently read an article which described a method to track memory leaks. I am having a little trouble following part of it and was hoping someone could help me.
The first step is to use #define statements to replace all the calls to new with a debug-friendly new:
#ifdef _DEBUG
#define DEBUG_NEW new(__FILE__, __LINE__)
#else
#define DEBUG_NEW new
#endif
#define new DEBUG_NEW
Then the debug-friendly new is defined:
#ifdef _DEBUG
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
But, in this code, since we defined the debug-friendly new as new(__FILE__, __LINE) then wont calls like:
int *ptr = new int;
be replaced with:
int *ptr = new(___FILE__,__LINE__) int;
when what we really need is:
int *ptr = new(sizeof(int), __FILE__,__LINE__);
Thanks
Todd
Because it''s doing operator overloading for the new operator I assume the compiler will see the int bit work out it''s size and pass it as the first parameter. This is because the proper new has to be declared somewhere and that will look something like
inline void * __cdecl operator new(unsigned int size);
I could be wrong though, I''m just guessing
inline void * __cdecl operator new(unsigned int size);
I could be wrong though, I''m just guessing
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement