Diagnostic - memory leak analyzer


Integration into your own project / process.

diagnostic.dll must be loaded once into process, where you try to search memory leaks.

Following code snipet shows how this could be done from C or C++:

#include "DiagnosticControl.h"

After this it's possible to use any diagnostic control using global variable g_diagnosticClient.

Similar thing can be done also from C# like this:

class Diagnostic
{ 
    [DllImport("Diagnostic.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern void InitDiagnostic();
} 
...

Diagnostic.InitDiagnostic();

Once diagnostic.dll is loaded and initialized it can find memory leaks from any other dll within same process.

It's recommended to have all .dll's and main .exe with .pdb - program database files supplied. .pdb's are optional, but they are quite important because they give exact source code path and line where memory leak occurred. Dll itself does not need to be in debug mode or have special kind of run-time - .pdb can be generated for release binaries as well.
Diagnostic.dll will monitor all .dll's within a process indepentetly whether they were compiled in debug / release mode.

So it's possible also to integrate Diagnostic.dll also into production application - and activate later on - when needed.
We recommend to perform such intergation once, but not to ship Diagnostic.dll with your installation until it's needed. When there comes such need to analyze what goes wrong - deploy Diagnostic.dll along with description how to use it.

Approach #1: Enable memory leak detection - use LeakDetectControl.exe

Simplest way to enable or disable memory leak detection is to use tool - LeakDetectControl.exe provided with zip package.

Approach #2: Enable memory leak detection - code memory leak activation by yourself using C++.

Include additional header file, which can be found from release zip package.

#include "DiagnosticControl.h" //g_diagnosticClient

Use StartStopMonitor to start or stop memory leak monitoring, and DumpMemoryLeaks to produce report on memory leaks, TestDiagnostic.cpp is provided as an example of such demo application - listed also here:

#include "DiagnosticControl.h" //g_diagnosticClient
#include #include "MyTestClass.h"

void main(void)
{
    // Start memory leak monitoring. 
    g_diagnosticClient.StartStopMonitor(true);
    
    // Do stuff which potentially loses some memory.
    CMyTestClass testx;
    testx.InTestMethod1();

    // Stop memory leak monitoring.
    g_diagnosticClient.StartStopMonitor(false);

    // Delete old report if any.
    DeleteFileA("memoryLeaks.txt");

    // Dump report of leaking memory of top 100 memory leaks.
    wchar_t errInfo[1024] = { 0 };

    if( !g_diagnosticClient.DumpMemoryLeaks(L"memoryLeaks.txt", 100, errInfo, 1024) )
    { 
        _putws(errInfo);
    } else {
        // Display report back to console. (Or use other means to display it)
        system("type memoryLeaks.txt");
    }
}

Approach #3: Enable memory leak detection - code memory leak activation by yourself using C#.


DiagnosticIpcM ipc = new DiagnosticIpcM();

Start memory leak monitoring:

ipc.StartStopMonitor(true);

Stop it:

ipc.StartStopMonitor(false);

Write memory leak report into file, by picking up top 100 memory consumers:

ipc.DumpMemoryLeaks( "memoryLeaks_log.txt", 100);

Clean up staticstics:

ipc.Reset();