I'm trying to write a memory profiler and so far have been able to get my custom functions to work for malloc, free, new and delete.
I tried using __FILE__
and __LINE__
to log the originator inside the overloaded new method, but (as expected) it just gives the details of where the overloaded function is.
Is there a way to get the details about the originator to the overloaded functions without doing any changes to existing code of the component being tested (like #define for malloc)?
The function I'm using is:
void* operator new (size_t size)
{
if(b_MemProfStarted)
{
b_MemProfStarted = false;
o_MemLogFile << "NEW: " << "| Caller: "<< __FILE__ << ":"
<< __LINE__ << endl;
b_MemProfStarted = true;
}
void *p=malloc(size);
if (p==0) // did malloc succeed?
throw std::bad_alloc(); // ANSI/ISO compliant behavior
return p;
}
The bool b_MemProfStarted is used to avoid recursive calls on ofstream and map.insert.