H
hbdevelop1
Hello,
I have written a memory tracker to prevent memory leaks in my programs.
The memory tracker meets the following requirements:
r1-Allocate memory using the system's new/new[].
r2-Deallocate memory using the system's delete/delete[].
r3-Log the file name and line where an allocation happens.
r4-When a memory is fred, remove the memory allocation from the log.
r5-At application exit, display the log.
Please find the code at the end of the email.
I have the following questions :
1-
g++ issues the following warning in my operator delete/delete[]: memtracker4.cpp:24:9: warning: deleting ‘void*’ is undefined [enabled by default]
delete p;
^
Do you have any idea how to correct the code so I don't get the warning ?
2-
To remove tracks of memory added in my operator new/new[], I am using template functions deleteArray and deleteObj.
But I remember in one company I worked in, they were using new(__FILE__,__LINE__) or new[](__FILE__,__LINE__) for allocations and plain delete/delete[] for deallocation.
And I wonder now, how they were removing memory tracks added by their operator new/new[] since they were using plain delete or delete[] for deallocation.
Could anybody please tell me if this is possible ? or were they not removing tracks ?
3-
Please tell me anything else to improve this code.
/////// code ////////
#include <stdio.h>
#include <new>
void AddToLog(long ptr, size_t size,const char * filename, int line)
{
printf("allocation : 0x%08X,%d,%s,%d\n",ptr, size,filename, line);
}
void RemoveFromLog(long ptr)
{
printf("deallocation : 0x%08X\n",ptr);
}
void * operator new(size_t size, const char *filename, int line)
{
void *ptr = :perator new(size);
AddToLog((long)ptr, size, filename, line);
return(ptr);
};
void * operator new[](size_t size, const char *filename, int line)
{
void *ptr = :perator new[](size);
AddToLog((long)ptr, size, filename, line);
return(ptr);
};
void operator delete(void *p, const char *filename, int line)
{
RemoveFromLog((long)p);
delete p; //g++ outputs warning: deleting ‘void*’ is undefined [enabled by default]
};
void operator delete[](void *p, const char *filename, int line)
{
RemoveFromLog((long)p);
delete [] p; //g++ outputs warning: deleting ‘void*’ is undefined [enabled by default]
};
template<class T> void deleteObj(T *p)
{
RemoveFromLog((long)p);
delete p;
};
template<class T> void deleteArray(T *p)
{
RemoveFromLog((long)p - sizeof(unsigned int));
delete [] p;
};
struct O11
{
int x;
public:
~O11 (){}
};
#define new new(__FILE__,__LINE__)
int main()
{
char *c=new char;
deleteObj<char>(c);
O11 *o=new O11;
deleteObj<O11>(o);
O11 *o1=new O11[10];
deleteArray<O11>(o1);
char *c3=new char[3];
deleteObj<char>(c3);
/*Note that I am treating c3 as a simple object not as array
(which caused my question at http://stackoverflow.com/questions/21178252/c-book-detailing-delete-and-delete)
*/
return 0;
}
I have written a memory tracker to prevent memory leaks in my programs.
The memory tracker meets the following requirements:
r1-Allocate memory using the system's new/new[].
r2-Deallocate memory using the system's delete/delete[].
r3-Log the file name and line where an allocation happens.
r4-When a memory is fred, remove the memory allocation from the log.
r5-At application exit, display the log.
Please find the code at the end of the email.
I have the following questions :
1-
g++ issues the following warning in my operator delete/delete[]: memtracker4.cpp:24:9: warning: deleting ‘void*’ is undefined [enabled by default]
delete p;
^
Do you have any idea how to correct the code so I don't get the warning ?
2-
To remove tracks of memory added in my operator new/new[], I am using template functions deleteArray and deleteObj.
But I remember in one company I worked in, they were using new(__FILE__,__LINE__) or new[](__FILE__,__LINE__) for allocations and plain delete/delete[] for deallocation.
And I wonder now, how they were removing memory tracks added by their operator new/new[] since they were using plain delete or delete[] for deallocation.
Could anybody please tell me if this is possible ? or were they not removing tracks ?
3-
Please tell me anything else to improve this code.
/////// code ////////
#include <stdio.h>
#include <new>
void AddToLog(long ptr, size_t size,const char * filename, int line)
{
printf("allocation : 0x%08X,%d,%s,%d\n",ptr, size,filename, line);
}
void RemoveFromLog(long ptr)
{
printf("deallocation : 0x%08X\n",ptr);
}
void * operator new(size_t size, const char *filename, int line)
{
void *ptr = :perator new(size);
AddToLog((long)ptr, size, filename, line);
return(ptr);
};
void * operator new[](size_t size, const char *filename, int line)
{
void *ptr = :perator new[](size);
AddToLog((long)ptr, size, filename, line);
return(ptr);
};
void operator delete(void *p, const char *filename, int line)
{
RemoveFromLog((long)p);
delete p; //g++ outputs warning: deleting ‘void*’ is undefined [enabled by default]
};
void operator delete[](void *p, const char *filename, int line)
{
RemoveFromLog((long)p);
delete [] p; //g++ outputs warning: deleting ‘void*’ is undefined [enabled by default]
};
template<class T> void deleteObj(T *p)
{
RemoveFromLog((long)p);
delete p;
};
template<class T> void deleteArray(T *p)
{
RemoveFromLog((long)p - sizeof(unsigned int));
delete [] p;
};
struct O11
{
int x;
public:
~O11 (){}
};
#define new new(__FILE__,__LINE__)
int main()
{
char *c=new char;
deleteObj<char>(c);
O11 *o=new O11;
deleteObj<O11>(o);
O11 *o1=new O11[10];
deleteArray<O11>(o1);
char *c3=new char[3];
deleteObj<char>(c3);
/*Note that I am treating c3 as a simple object not as array
(which caused my question at http://stackoverflow.com/questions/21178252/c-book-detailing-delete-and-delete)
*/
return 0;
}