Jump to content
  • Advertisement
Funkymunky

DX12 Memory Leak Detection and DX12

Recommended Posts

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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 . 

Share this post


Link to post
Share on other sites
Posted (edited)

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).
 

Edited by Steve_Segreto

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!