F
foo
I'm creating a debug class called debug_mem_allocation for the purpose
of finding memory leaks.
I used macro's to replace the new and delete operators.
My problem is with trying to replace the delete[] operator with a
macro.
I can't replace the delete[] operator by using void* as the first
parameter, because then my code will not be able to modify the calling
function's pointer, nor would it get the source file name and line
number.
My current work around has been to modify the source code by replacing
delete[] with DELETE_ARRAY_AND_NULLIFY, which is defined as delete[]
for NON-DEBUG compile, and defined as an operator-() of my debug class
for DEBUG compile.
Can anyone think of a better way to replace the delete[] operator via
macro that would not require me to modify the source code?
My compilers are VC++ 6.0 and GNU 3.x
Here's the header:
class debug_mem_allocation
{
public:
enum ACTION_SWITCH{AllocateMem, DeAllocMem, GenerateReport};
debug_mem_allocation(const char* FileName, int LineNo, const char*
FunctionName)
:m_FileName(FileName), m_LineNo(LineNo), m_FuncName(FunctionName){}
template<typename T>
void operator+(T &ptr)
{
Log(ptr, DeAllocMem);
delete ptr;
ptr = NULL;
}
template<typename T>
void operator+(const T &ptr)
{
Log(ptr, DeAllocMem);
delete ptr;
}
template<typename T>
void operator-(T &ptr)
{
Log(ptr, DeAllocMem);
delete [] ptr;
ptr = NULL;
}
template<typename T>
T& operator<<(T &ptr)
{
Log(ptr, AllocateMem);
return ptr;
}
template<typename T>
void free_and_nullify(T &memblock , const char* VariableName)
{
Log(memblock, DeAllocMem);
free(memblock);
memblock = NULL;
}
void *malloc_and_log(size_t size );
void *realloc_and_log(void *memblock, size_t size );
static void SendOutCurrentLog();
private:
void Log(void const * const Ptr, ACTION_SWITCH Sw);
const char* m_FileName;
const int m_LineNo;
const char* m_FuncName;
};
#if (defined(DEBUG) || defined (_DEBUG)) &&
defined(USE_DEBUG_MEM_ALLOCATION)
#define new debug_ext::debug_mem_allocation(__FILE__, __LINE__,
__FUNCTION__)<< new
#define delete debug_ext::debug_mem_allocation(__FILE__,
__LINE__, __FUNCTION__)+
#define DELETE_ARRAY_AND_NULLIFY debug_ext::debug_mem_allocation(__FILE__,
__LINE__, __FUNCTION__)-
#define malloc(x) debug_ext::debug_mem_allocation(__FILE__,
__LINE__, __FUNCTION__).malloc_and_log(x)
#define realloc(m,s) debug_ext::debug_mem_allocation(__FILE__,
__LINE__, __FUNCTION__).realloc_and_log(m,s)
#define free(x) debug_ext::debug_mem_allocation(__FILE__,
__LINE__, __FUNCTION__).free_and_nullify(x, #x)
#else
#define DELETE_ARRAY_AND_NULLIFY delete []
#endif
of finding memory leaks.
I used macro's to replace the new and delete operators.
My problem is with trying to replace the delete[] operator with a
macro.
I can't replace the delete[] operator by using void* as the first
parameter, because then my code will not be able to modify the calling
function's pointer, nor would it get the source file name and line
number.
My current work around has been to modify the source code by replacing
delete[] with DELETE_ARRAY_AND_NULLIFY, which is defined as delete[]
for NON-DEBUG compile, and defined as an operator-() of my debug class
for DEBUG compile.
Can anyone think of a better way to replace the delete[] operator via
macro that would not require me to modify the source code?
My compilers are VC++ 6.0 and GNU 3.x
Here's the header:
class debug_mem_allocation
{
public:
enum ACTION_SWITCH{AllocateMem, DeAllocMem, GenerateReport};
debug_mem_allocation(const char* FileName, int LineNo, const char*
FunctionName)
:m_FileName(FileName), m_LineNo(LineNo), m_FuncName(FunctionName){}
template<typename T>
void operator+(T &ptr)
{
Log(ptr, DeAllocMem);
delete ptr;
ptr = NULL;
}
template<typename T>
void operator+(const T &ptr)
{
Log(ptr, DeAllocMem);
delete ptr;
}
template<typename T>
void operator-(T &ptr)
{
Log(ptr, DeAllocMem);
delete [] ptr;
ptr = NULL;
}
template<typename T>
T& operator<<(T &ptr)
{
Log(ptr, AllocateMem);
return ptr;
}
template<typename T>
void free_and_nullify(T &memblock , const char* VariableName)
{
Log(memblock, DeAllocMem);
free(memblock);
memblock = NULL;
}
void *malloc_and_log(size_t size );
void *realloc_and_log(void *memblock, size_t size );
static void SendOutCurrentLog();
private:
void Log(void const * const Ptr, ACTION_SWITCH Sw);
const char* m_FileName;
const int m_LineNo;
const char* m_FuncName;
};
#if (defined(DEBUG) || defined (_DEBUG)) &&
defined(USE_DEBUG_MEM_ALLOCATION)
#define new debug_ext::debug_mem_allocation(__FILE__, __LINE__,
__FUNCTION__)<< new
#define delete debug_ext::debug_mem_allocation(__FILE__,
__LINE__, __FUNCTION__)+
#define DELETE_ARRAY_AND_NULLIFY debug_ext::debug_mem_allocation(__FILE__,
__LINE__, __FUNCTION__)-
#define malloc(x) debug_ext::debug_mem_allocation(__FILE__,
__LINE__, __FUNCTION__).malloc_and_log(x)
#define realloc(m,s) debug_ext::debug_mem_allocation(__FILE__,
__LINE__, __FUNCTION__).realloc_and_log(m,s)
#define free(x) debug_ext::debug_mem_allocation(__FILE__,
__LINE__, __FUNCTION__).free_and_nullify(x, #x)
#else
#define DELETE_ARRAY_AND_NULLIFY delete []
#endif