imgui: Memory leak when rendering with DX11 in C++

Version/Branch of Dear ImGui:

Version 1.89.6, Branch: master

Back-ends:

imgui_impl_win32.cpp + imgui_impl_dx11.cpp

Compiler, OS:

Windows 10 + MSVC 14.39.33519

Full config/build information:

No response

Details:

My Issue/Question:

I am using Dear ImGui with the win32 and dx11 backends in a fairly complicated control panel application, and it appears to be causing a memory leak. The code I’m using to render is straight from the demo: image Every frame, imgui_new_frame() is called, then our UI code, then imgui_dx11_render(). However, at a regular interval 32,903 bytes are allocated and never deallocated. In one test it was every ~8 seconds, and in another every ~700 milliseconds: image image Line 215 is ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());. I’m not able to post the full code because there are thousands of lines and it’s confidential, so I understand that it may be difficult to help, but it would be helpful to know:

  1. Are there any common/obvious/possible mistakes to make between imgui_new_frame and imgui_dx11_render that could cause this?
  2. Is it possible/likely that the call stack provided by VS2022 is wrong or misleading, and that something else is at fault?
  3. Is there anything I can try to get more information on what’s going on to help diagnose the issue?

It’s very strange because the code works exactly like the example, and I doubt that Dear ImGui or DX11 have significant memory leaks like this. The fact that it happens at a consistent interval but not every frame also seems odd.

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

// Here's some code anyone can copy and paste to reproduce your issue
void imgui_new_frame() {
    if (g_ResizeWidth != 0 && g_ResizeHeight != 0)
    {
        CleanupRenderTarget();
        g_pSwapChain->ResizeBuffers(0, g_ResizeWidth, g_ResizeHeight, DXGI_FORMAT_UNKNOWN, 0);
        g_ResizeWidth = g_ResizeHeight = 0;
        CreateRenderTarget();
    }
    // Start the Dear ImGui frame
    ImGui_ImplDX11_NewFrame();
    ImGui_ImplWin32_NewFrame();
    ImGui::NewFrame();
}

void imgui_dx11_render() {
    ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.00f, 1.00f);
    // Rendering
    ImGui::Render();
    const float clear_color_with_alpha[4] = { clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w };
    g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, nullptr);
    g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, clear_color_with_alpha);
    ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
    g_pSwapChain->Present(1, 0);
}

imgui_new_frame();
state.update(target_seconds_per_frame);    
imgui_dx11_render();

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (9 by maintainers)

Commits related to this issue

Most upvoted comments

There’s an alternative API to get occluded notifications, but I couldn’t get it to work either. I only get 2 notifications at the start of the app, but then nothing. That being said, I’m not sure at all how to use it correctly, I couldn’t find any decent documentation about it, so here’s the gist of it if you wanna try by yourself:

#include <d3d11_1.h>

#define WM_OCCLUSION_EVENT (WM_USER + 1)

//Data
[...]
static IDXGIFactory2* g_pFactory = nullptr;
[...]

int main(int, char**)
{
[...]
    // Setup Platform/Renderer backends
    ImGui_ImplWin32_Init(hwnd);
    ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext);

    DWORD occlusionStatusCookie = 0;
    g_pFactory->RegisterOcclusionStatusWindow(hwnd, WM_OCCLUSION_EVENT , &occlusionStatusCookie);
[...]
    g_pFactory->UnregisterOcclusionStatus(occlusionStatusCookie);
    
     // Cleanup
    ImGui_ImplDX11_Shutdown();
    ImGui_ImplWin32_Shutdown();
    ImGui::DestroyContext();
[...]
}

[...]

bool CreateDeviceD3D(HWND hWnd)
{
[...]
    IDXGIDevice * pDXGIDevice;
    res = g_pd3dDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&pDXGIDevice);
    if (res != S_OK)
        return false;

    IDXGIAdapter * pDXGIAdapter;
    res = pDXGIDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&pDXGIAdapter);
    if (res != S_OK)
        return false;

    res = pDXGIAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&g_pFactory);
    if (res != S_OK)
        return false;
[...]
}

LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
[...]
    switch (msg)
    {
    case WM_OCCLUSION_EVENT:
    {
        g_SwapChainOccluded = !g_SwapChainOccluded; // Not sure how we're supposed to tell when it's occluded or not? I guess it alternate, but I'm not sure about that at all.
        break;
    }
[...]
}

@ocornut You may already know it, but just in case: I saw on Twitter that you were starting to use the dxgi flip model, but it will never return DXGI_STATUS_OCCLUDED. So if you use DXGI_SWAP_EFFECT_FLIP_DISCARD in the backends, this issue will come back.

I have pushed ec1d2be which implement this for the DX9, DX10, DX11 and DX12 examples

DX9 and DX12 are both slightly different, and Present() does wait for vsync even on fail so it didn’t exhibit a CPU core burn, but adding better support for it means we are not looping when screen locked which is desirable.

If you want to dig further into remaining issues please do but I’d prefer if you do it (or confirm it) in vanilla example. I don’t know if there are ways to break into those allocs.

Your attempt as pictured in screenshot will let the rest of the code run at max speed, burning a core and essentially making no difference.

It does occur in the win32 dx11 example: image The big jump in heap size is where it was locked. The call stack again points to ImGui_ImplDX11_RenderDrawData(): image

I’ll test the snippet here based on the Present() return value. I’m not familiar with how multiple viewports work so I’m not sure what an appropriate solution for the library as a whole is, but if that works it should solve the problem in my case. Thanks!

Definitely seems to be a result of the screen being locked. I tried disabling rendering when g_pSwapChain->Present(1, 0); returns DXGI_STATUS_OCCLUDED, and it drastically reduced the issue. Left shows before the change, right after, and the 5min gap between snapshots is where the screen was locked: image Now to figure out how to detect when it’s no longer occluded. I tried checking GetClipBox, but it doesn’t return NULLREGION while locked for some reason.