5

I am trying to write a small memory leak detection tool.

My idea is to track the dynamic memory allocation life with in my application to determine any invalid access of memory or un deleted memory which might cause my application to core over a time of use.

I want to write a simple interface to override new and delete.

And in my overridden new i wanted to print function line address etc. And then call the standard new.

Have anyone already tried this? I am not sure whether i can call standard new from my class specific new operator.

avakar
  • 32,009
  • 9
  • 68
  • 103
linux developer
  • 821
  • 1
  • 13
  • 34
  • 3
    The simplest solution: `cd /project/src; fgrep --silent -r -e "new" && "Write better code"` – Kerrek SB Nov 15 '12 at 16:45
  • Vaguely related, and mostly Windows-specific, but it's odd that your question appears moments after I read Raymond Chen's blog post on writing your own allocators: http://blogs.msdn.com/b/oldnewthing/archive/2012/11/15/10368691.aspx – icabod Nov 15 '12 at 16:46
  • 1
    possible duplicate of [Immediate detection of heap corruption errors on Windows. How?](http://stackoverflow.com/questions/12724057/immediate-detection-of-heap-corruption-errors-on-windows-how) – Sergey K. Nov 15 '12 at 16:46
  • @SergeyK, it's most certainly not a duplicate of your question. – avakar Nov 15 '12 at 16:50
  • possible duplicate of [Critique my non-intrusive heap debugger](http://stackoverflow.com/questions/2835416/critique-my-non-intrusive-heap-debugger) – fredoverflow Nov 15 '12 at 18:25
  • Use a tool like valgrind. – vonbrand Mar 18 '13 at 02:29

4 Answers4

0

Calling ::operator new from a class-specific operator new is fine, and quite common. You can't call global operator new from a replacement version of ::operator new because if you replace the global operator new the old one doesn't exist.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • @PiotrNycz - `mallow`? ` Sure, `malloc` exists. That doesn't change the fact that if you replace the global operator new you can't call the old one. – Pete Becker Nov 15 '12 at 18:09
  • I was typing from phone, of course `malloc`. You can call `malloc` from new operator new... For old operator new - I am not sure, never tried this, but maybe `dlsym(RTLD_NEXT, "name")` could be used for retrieving old operator new - need to check with `nm` the real (mangled name) of operator new... But as I said never used dlsym() for C++ functions, only for C functions... – PiotrNycz Nov 15 '12 at 20:40
0

In this question i have proposed an ad-hoc solution to your problem: Immediate detection of heap corruption errors on Windows. How?

In general, you can replace your new and delete with this code:

DWORD PageSize = 0;

inline void SetPageSize()
{
    if ( !PageSize )
    {
        SYSTEM_INFO sysInfo;
        GetSystemInfo(&sysInfo);
        PageSize = sysInfo.dwPageSize;
    }
}

void* operator new (size_t nSize)
{
    SetPageSize();
    size_t Extra = nSize % PageSize;
    nSize = nSize + ( PageSize - Extra );
    return Ptr = VirtualAlloc( 0, nSize, MEM_COMMIT, PAGE_READWRITE);
}

void operator delete (void* pPtr)
{
    MEMORY_BASIC_INFORMATION mbi;
    VirtualQuery(pPtr, &mbi, sizeof(mbi));
    // leave pages in reserved state, but free the physical memory
    VirtualFree(pPtr, 0, MEM_DECOMMIT);
    DWORD OldProtect;
    // protect the address space, so noone can access those pages
    VirtualProtect(pPtr, mbi.RegionSize, PAGE_NOACCESS, &OldProtect);
}

to determine invalid access of memory. In the further discussion there you will find ideas for leaks detection and detection of other memory errors.

If you want to call global new and delete, you can use :: global namespace prefix:

return ::new(nSize);
Community
  • 1
  • 1
Sergey K.
  • 24,894
  • 13
  • 106
  • 174
0

The described approach sounds reasonable. You can enhance it and store some bits of identifying data on each allocation into some data structure. This way you can track all the non-freed memory upon program termination without the need to inspect all allocation/deallocation logs.

In order to detect corruptions you can wrap each allocation with paddings before the start and after the end and fill it with some predefined pattern. Upon memory freeing you can verify that the patterns are still in place. This will give you a good chance of detecting corruption.

You can call global new with scope prefix: ::new.

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85