Memory Leak Detection and DX12

Started by
5 comments, last by cozzie 5 years ago

I've gotten used to using Visual Leak Detector on another program and would like to migrate it to my DX12 code.  It's nice because after running my code, I can check the console output to see if any memory leaks popped up.  I've used both Visual Leak Detector and just the CRT Heap detection stuff.  Both work fine in my other programs, but in my DX12 program I get no output on the console (even if I manually introduce a memory leak).

Using the CRT model, you can either call _CrtSetDbgFlag to trigger output on exits points, or call  _CrtDumpMemoryLeaks before exiting the program.  That first option doesn't work, and the second isn't that great because anything that hits a destructor after its called will be incorrectly flagged as a leak.

Anyone have any lucky using memory leak detection with DirectX 12?  I'd prefer if it printed to the console rather than running as a separate program, and I'd really prefer something free...

Advertisement

You can enable the D3D Debuglayer and try to use PIX, it will give you some hint.

You also can set name on your d3d objects and print some info at a special time.

Ah, but I don't just want to see a listing of DirectX object lifetime problems, I want to see general heap memory leaks as well.

I am not sure if that carries over to DirectX since it's built around COM . The best I can think of as mentioned already is enabling the debug layer and making a call to ReportLiveObjects() after you've released all dx objects to see if you have live objects . 

As far as I'm aware native heap leak detection should still be possible using the canonical ways even with a DX12 context in the process.

Here's how I usually use the C-Runtime Debug library to help me detect these heap leaks in Windows using that first option (and it does work for me): (https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/crtsetdbgflag?view=vs-2017).

 


//
// C-Runtime Debug Library
//
#define _CRTDBG_MAP_ALLOC  
#include <stdlib.h>  
#include <crtdbg.h>
  
#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif

INT WINAPI WinMain (
    HINSTANCE h,
    HINSTANCE prevInstace,
    LPSTR lpCmdLine,
    int nShowCmd
    )
{

    // Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)
    _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif

    int* leaked = new int[5000];
    return 0;
}

Which produces this kind of output:

Detected memory leaks!
Dumping objects ->
..\src\Main.cpp(26) : {2208} normal block at 0x01B44C20, 20000 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
Object dump complete.
The program '[180644] Win32Program_debug.exe' has exited with code 0 (0x0).
 

In my experience VLD should work fine besides DX. Like suggested about, best would be to combine it with the DX/D3D debug layer.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement