Memtracer

Introduction

Tools that track memory allocations are rare. The first that comes to mind is Visual Leak Detector. While it is handy, it has two drawbacks: it only works with application runned in debug mode and it only reports leaks.

This tool aims :

  • To build a complete trace of allocation for application in debug and in retail/release
  • To keep track of every operation on the heap
  • To track memory at any time during application lifetime

Optionally, it counts as feature

  • It can log from multiple thread
  • Logging has minimal impact on performances

You can build the solution with Visual Studio 2005. The logger only works with 32 bit x86 applications.

This implementation follows the guidelines given by Jelle in his fascinating article :

http://www.gamasutra.com/view/feature/1430/monitoring_your_pcs_memory_usage_.php

How to use it

Setup your project(s)

In order to force your projects recording valid stack frames even in Release, you need to :

  • Add the file memtracer_prologepilog.cpp to the project
  • Add /Gh /gh in "compiler option"
  • Make sure the project has "Debug Information Format: Program Database" /Zi (else symbol resolution will fail, nb: /C7 or /ZI should be ok)

Note: you do not have to do that for every project in the solution, only for the main/needed ones.

Load MemWatcher.dll and invoke MemInit() method

There's many way to do that, you can just add the lines :

HMODULE Lib = LoadLibrary(L"memwatcher.dll");

// Invoke MemInit();
typedef void (TInitFunc)(void);
TInitFunc *MemInit = (TInitFunc *) GetProcAddress(Lib, "MemInit");
if( MemInit != 0 ) MemInit();

somewhere in the main function

Don't forget to "#include <windows.h>"

You can shutdown the Logging by unloading the library.

Start your project

Important: Place "MemWatcher.dll" and "detoured.dll" in same directory than the executable

When your project loads "MemWatcher.dll", the application waits for a connection.
Open MemTracer and connect to (the computer running) the application (most likely localhost).
Once the connection is done, the application can continue.

Warning for XP users

On some versions of Windows XP, dbghelp.dll seems to be outdated. If this is the case, Memtracer will fail at displaying a correct callstack.

To fix this problem, you should download a newer version of dbghelp at

http://www.microsoft.com/whdc/devtools/debugging/default.mspx

and place the freshly installed dbghelp.dll into your project folder next to the executable.

Screenshots

{"module":"wiki\/image\/FlickrGalleryModule","params":{"userName":"supzim","size":"large","disableBrowsing":"yes"}}

Download

Acknowledgment

Jelle for his great article
Mischief for the original implementation, inspiration, help, support and finding bugs

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License