I have a very big project. I am trying to monitor the memory allocated and deallocated. Here is the sample program I tried. However, I see that it just prints the function name of new, which I understand. The question is how can I print the function name, line number of the caller.
main.cpp
:
#include <QtCore/QCoreApplication>
#include <cstdlib>
#include <stdio.h>
#include <fstream>
#include <memOperation.h>
#include <DumpMemory.h>
#define BUFFER (4)
class MemPlay;
#define LOG_STRING()\
{\
std::ofstream dumpfile; \
dumpfile.open("/export/home/joshis1/DBG_REC.log"); \
dumpfile<<"FUNC = "<<__FUNCTION__<<"LINE = "<<__LINE__<<std::endl; \
dumpfile.close(); \
}
void* operator new(std::size_t sz)
{
void *mem = std::malloc(sz + BUFFER );
memset(mem+sz,'PACE',4);
LOG_STRING();
return mem;
}
void operator delete(void* ptr)
{
std::free(ptr);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MemPlay *pMemPlay1 = new MemPlay();
pMemPlay1->MyMemPlay();
return a.exec();
}
memOperation.h
#include "QDebug"
class MemPlay
{
public:
void MyMemPlay()
{
qDebug()<<"My Mem Play";
char *t = new char[10] ;
strcpy(t,"SHREYASJOSHI_SAYS_HELLO_WORLD_AND_CORRUPTS_MEMORY");
}
void FreeMemPlay(void *t)
{
delete t;
}
};
Here was the erroneous result -
FUNC = operator newLINE = 25