LeakChecker Manual
For latest news, please visit
http://www.leakchecksoft.comTo locate leak source code precisely, LeakChecker should only link with "Debug"
build.
Quick Start
1. Double click "install_vs2005.js" or "install_vs2008.js" to install
LeakChecker.
2. Add following code at the start function.
For a console program, the best place is right after the main function:
#ifdef _DEBUG
#include
"LeakChecker.h"
#pragma
comment(lib,
"LeakChecker")
#endif
int _tmain(int
argc,
_TCHAR* argv[]) // this
function will be called at very first
{
#ifdef _DEBUG
LeakCheckerStart(); <-- Add this line
#endif
For a MFC program:
CDemoMFCApp::CDemoMFCApp()
// because there is a global static CDemoMFCApp
instance,
// so the construct function will be called at very first
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
#ifdef _DEBUG
LeakCheckerStart(); <-- Add this line
#endif
}
CDemoMFCApp theApp; // global static CDemoMFCApp instance
3. Compile & Link with "Debug" build.
4. Copy
LeakChecker\dbghelp.dll to the folder contains your executable file. For VC, this
will be the "Debug" folder.
5. Press "F5" to run your program in debug mode.
6. After program exits, leak information will be dumped in the "Output" pane.
7. Double click the filename in "Output" pane to locate the source code line
which caused leaks.
Note: For step 1, if you are not using VS2005 or VS2008, copy LeakChecker.h &
LeakChecker.lib to the source code folder, so VC can find and link it.
For step 4, if you are building 64bit application, copy
LeakChecker\x64\dbghelp.dll to "x64\Debug" folder.
Function Level Leak Detection
Sometimes we have some one-time memory allocation. So in fact these are not real
memory leaks.
It will be annoying to dump them because it will be made difficult to find the
real leaks.
So we can do a function level leak detection to avoid it.
Also this can be used to detect unintentional C++ object accumulation
which will be freed by container.
1. Add following code.
#ifdef _DEBUG
LeakCheckerSetOptions(16, FALSE, TRUE); <-- Add this line
LeakCheckerStart();
<-- Add this line
#endif
SuspectLeakFunction();
...
#ifdef _DEBUG
LeakCheckerStop(); <-- Add this line
LeakCheckerDump();
<-- Add this line
#endif
2. Compile & Link with "Debug" build.
3. Copy
dbghelp.dll to the folder contains your executable file. For VC, this
will be the "Debug" or "x64\Debug" folder.
4. Press "F5" to run your program in debug mode.
5. After program exits, leak information will be dumped in the "Output" pane.
6. Double click the filename in "Output" pane to locate the source code line
which caused leaks.
Note: If you didn't press "F5" to run your program, the leak
information will not be dumped to the "Output" pane. You will need to use tool
DebugView to
see it. You can get this tool from Microsoft website.
http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx
LeakChecker API
LeakCheckerStart function
The LeakCheckerStart function will start to monitor memory and
resource allocate and free operations. It affects global so only call this
function once for an application. No need to call it at every DLL's entry.
VOID
WINAPI
LeakCheckerStart();
LeakCheckerStop function
The LeakCheckerStop function will stop the monitoring.
VOID
WINAPI
LeakCheckerStop();
LeakCheckerDump function
The LeakCheckerDump function will dump the leaks to "Output" pane.
DWORD
WINAPI
LeakCheckerDump();
Return Value
If no leak, return zero. Otherwise, the number of leaks is returned.
LeakCheckerSetOptions function
The LeakCheckerSetOptions function sets the LeakChecker option. This
function should be called before function LeakCheckerStart.
VOID
WINAPI
LeakCheckerSetOptions(
DWORD dwCallStackFramesToCapture,
BOOL bBreakOnLeak,
BOOL bSingleThreadMode
);
Parameters
dwCallStackFramesToCapture
The number of frames to be captured when doing memory and resource
allocation. Must be less than 64. Default is 16.
bBreakOnLeak
If leak detected, LeakChecker will trigger a breakpoint. This gives you a
chance to examine the data of your program. Default is false.
bSingleThreadMode
By default, LeakChecker will monitor the whole program. By set this to TRUE,
LeakChecker will only monitor current thread.