D3D12 不可避免的泄漏报告

D3D12 unavoidable leak report

这个节目:

#include <d3d12.h>
#pragma comment(lib,"d3d12")

int main()
{
    ID3D12Debug *pDebug = NULL;
    D3D12GetDebugInterface(__uuidof(ID3D12Debug),(void**)&pDebug);
    pDebug->EnableDebugLayer();
    pDebug->Release();

    ID3D12Device *pDev = NULL;
    D3D12CreateDevice(NULL,D3D_FEATURE_LEVEL_12_1,__uuidof(ID3D12Device),(void**)&pDev);

    ID3D12DebugDevice *pDebugDevice = NULL;
    pDev->QueryInterface(&pDebugDevice);
    pDev->Release();
    pDebugDevice->ReportLiveDeviceObjects(D3D12_RLDO_DETAIL);
    pDebugDevice->Release();
}

在调试输出中给出:

D3D12 WARNING: Live ID3D12Device at 0x000C6BA8, Refcount: 2 [ STATE_CREATION WARNING #274: LIVE_DEVICE]
D3D12 WARNING:  Live ID3D12RootSignature at 0x000E62E8, Refcount: 0, IntRef: 2 [ STATE_CREATION WARNING #577: LIVE_ROOTSIGNATURE]
D3D12 WARNING:  Live ID3D12PipelineState at 0x0011C3C8, Refcount: 0, IntRef: 1 [ STATE_CREATION WARNING #572: LIVE_PIPELINESTATE]
D3D12 WARNING:  Live ID3D12PipelineState at 0x001421D8, Refcount: 0, IntRef: 1 [ STATE_CREATION WARNING #572: LIVE_PIPELINESTATE]
D3D12 WARNING:  Live ID3D12Resource at 0x00138FF8, Refcount: 0, IntRef: 1 [ STATE_CREATION WARNING #575: LIVE_RESOURCE]
D3D12 WARNING:  Live ID3D12Heap at 0x00144DD8, Refcount: 0, IntRef: 1 [ STATE_CREATION WARNING #579: LIVE_HEAP]

调试设备报告我创建的 D3D12 设备在我释放后仍然存在。我知道这确实是真的,因为调试设备本身实际上是使 D3D12 设备保持活动状态的唯一剩余引荐来源网址,但从我的角度来看,这不是 leak,因为我已正确发布我的 D3D12 设备。这只是对我的程序输出的污染,给出了我的代码中存在错误的错误指示。

我的问题是:我真的做错了吗?还是报告在 D3D12 调试设备中的工作方式不好?关于如何解决它的任何想法?

谢谢!

您应该尝试使用 D3D12_RLDO_IGNORE_INTERNAL 标志来忽略那些 RefCount 为 0 但仍具有 IntRef 的项目。

对于 'clean shutdown' 场景,我更喜欢使用 DXGI 调试设备报告而不是 Direct3D。

在我的 DeviceResources 实现中,我按如下方式创建 DXGI 设备:

    m_dxgiFactoryFlags = 0;

#if defined(_DEBUG)
    // Enable the debug layer (requires the Graphics Tools "optional feature").
    //
    // NOTE: Enabling the debug layer after device creation will invalidate the active device.
    {
        ComPtr<ID3D12Debug> debugController;
        if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(debugController.GetAddressOf()))))
        {
            debugController->EnableDebugLayer();
        }
        else
        {
            OutputDebugStringA("WARNING: Direct3D Debug Device is not available\n");
        }

        ComPtr<IDXGIInfoQueue> dxgiInfoQueue;
        if (SUCCEEDED(DXGIGetDebugInterface1(0, IID_PPV_ARGS(dxgiInfoQueue.GetAddressOf()))))
        {
            m_dxgiFactoryFlags = DXGI_CREATE_FACTORY_DEBUG;

            dxgiInfoQueue->SetBreakOnSeverity(DXGI_DEBUG_ALL, DXGI_INFO_QUEUE_MESSAGE_SEVERITY_ERROR, true);
            dxgiInfoQueue->SetBreakOnSeverity(DXGI_DEBUG_ALL, DXGI_INFO_QUEUE_MESSAGE_SEVERITY_CORRUPTION, true);
        }
    }
#endif

    ThrowIfFailed(CreateDXGIFactory2(m_dxgiFactoryFlags, IID_PPV_ARGS(m_dxgiFactory.ReleaseAndGetAddressOf())));

然后当我关机并想检查 'leaks':

#ifdef _DEBUG
    {
        ComPtr<IDXGIDebug1> dxgiDebug;
        if (SUCCEEDED(DXGIGetDebugInterface1(0, IID_PPV_ARGS(&dxgiDebug))))
        {
            dxgiDebug->ReportLiveObjects(DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_FLAGS(DXGI_DEBUG_RLO_SUMMARY | DXGI_DEBUG_RLO_IGNORE_INTERNAL));
        }
    }
#endif

For Win32 apps, there's a clear time to do 'clean exit' report leaks. For UWP apps, lifetime is controlled by PLM so you don't ever get a 'clean exit'--the process is just terminated after a suspend. For UWP, the best place to do leak checks is in the "device lost" handler caused by device removal errors.

使用 ComPtr 对象定义您的 D3D12 对象,如 Chuck 的示例:

ComPtr<ID3D12Debug> debugController;

这是避免泄漏所必需的,并且不需要执行 object->Release() 调用,因为 ComPtr 对象在其解构器中执行它们。