Hi ,
Thanks for prompt reply.
I was not aware that though "new" and "new[]" are different operators,
they are
translated in the same(similar) way by the compiler.
Consider using an existing library for your platform to look for leaks,
rather than writing your own. (Google for them.) Also, the thorough use
of smart pointers (e.g., std::auto_ptr, boost::scoped_ptr, and
boost::shared_ptr) virtually eliminates the possibility of memory
leaks. Consider using such practices.
I am looking into existing code, so replacing normal pointers with
std::auto_ptr,
boost::scoped_ptr is tedious job. Also I felt it would be a good
experience to do
it myself, than using a existing tool.
However, let me suggest that your approach of using __FILE__ and
__LINE__ won't work. See this recent discussion for details:
Dont know, why you say that. Please look at the code below. It seems to
work fine.
Mac OSX 10.4.2, gcc 4.0.1(Xcode)
Let me know if I am doing something wrong.
I call void "PrintMemoryDump()" at application Shutdown.
---------------------------------MemoryTest.h(Begin)--------------------------------------------------
#ifndef _MEMORY_TEST_H_
#define _MEMORY_TEST_H_
#include <new>
#include <stdio.h>
#include <stdlib.h>
void* operator new(std::size_t s, const char * file , int inLine) ;
void operator delete(void*) throw();
void* operator new[](std::size_t s, const char * file , int inLine);
void operator delete[](void*) throw();
void PrintMemoryDump();
#define DEBUG_NEW new(__FILE__, __LINE__)
#define new DEBUG_NEW
#endif
---------------------------------MemoryTest.h(End)--------------------------------------------------
---------------------------------MemoryTest.cpp(Begin)--------------------------------------------------
#include "MemoryTest.h"
#ifdef DEBUG_NEW
#define DEBUG_NEW new
#endif
struct tMemoryBlock
{
char _file[256];
int _lineNumber, _numBytes;
void * _pointer;
bool _deleted;
tMemoryBlock * next;
};
tMemoryBlock* head = NULL;
void AddBlock(tMemoryBlock ** inBlock)
{
if(head == NULL)
{
head = *inBlock;
return;
}
tMemoryBlock * ptr = head;
while(ptr ->next != NULL)
{
ptr = ptr->next;
}
ptr ->next = *inBlock;
}
void RemoveBlock(void * inptr)
{
if(head == NULL) return;
tMemoryBlock * ptr = head;
while(ptr != NULL)
{
if(ptr->_pointer == inptr)
{
ptr->_deleted = true;
}
ptr = ptr->next;
}
}
void PrintMemoryDump()
{
tMemoryBlock * ptr = head;
while(ptr != NULL)
{
if(ptr->_deleted == false)
{
fprintf(stderr, "**MEM LEAK** %s %d\n",ptr->_file,ptr->_lineNumber);
}
ptr =ptr->next;
}
ptr = head->next; tMemoryBlock * dptr= head;
while(ptr != NULL)
{
free(dptr);
dptr = ptr;
ptr = ptr->next;
}
}
void* operator new(std::size_t s, const char * file , int inLine)
{
void *ptr = (void *)malloc(s);
tMemoryBlock * t = (tMemoryBlock*)malloc(sizeof(tMemoryBlock));
strcpy(t->_file, file);
t->_lineNumber = inLine;
t->_deleted = false;
t->_numBytes = s;
t->next = NULL;
t->_pointer = ptr;
AddBlock(&t);
return(ptr);
}
void operator delete(void* p) throw()
{
RemoveBlock(p);
free(p);
}
void* operator new[](std::size_t s, const char * file , int inLine)
{
void *ptr = (void *)malloc(s);
tMemoryBlock * t = (tMemoryBlock*)malloc(sizeof(tMemoryBlock));
strcpy(t->_file, file);
t->_lineNumber = inLine;
t->_deleted = false;
t->_numBytes = s;
t->next = NULL;
t->_pointer = ptr;
AddBlock(&t);
return(ptr);
}
void operator delete[](void*p) throw()
{
RemoveBlock(p);
free(p);
}
#define new DEBUG_NEW
#define DEBUG_NEW new(__FILE__, __LINE__)
---------------------------------MemoryTest.cpp(End)--------------------------------------------------