具有 0 个引用的 DirectX11 COM 对象未释放

DirectX11 COM object with 0 references not released

我正在尝试追踪一个 "memory leak." 但是它看起来不像是真正的内存泄漏,因为调用 ReportLiveDeviceObjects 报告有 0 个引用

D3D11 WARNING:  Live ID3D11Texture2D at 0x00000140D3FE44F0, Refcount: 0, IntRef: 1 [ STATE_CREATION WARNING #425: LIVE_TEXTURE2D]
D3D11 WARNING:  Live ID3D11RenderTargetView at 0x00000140D3FCBB60, Refcount: 0, IntRef: 0 [ STATE_CREATION WARNING #428: LIVE_RENDERTARGETVIEW]
D3D11 WARNING:  Live ID3D11Texture2D at 0x00000140D3FE5BF0, Refcount: 0, IntRef: 1 [ STATE_CREATION WARNING #425: LIVE_TEXTURE2D]
D3D11 WARNING:  Live ID3D11RenderTargetView at 0x00000140B8EDB000, Refcount: 0, IntRef: 0 [ STATE_CREATION WARNING #428: LIVE_RENDERTARGETVIEW]

如您所见,ID3D11RenderTargetView 对象的内部和外部引用均为 0。然而它仍然是一个活的对象。什么会导致这种情况发生?

为清楚起见,我通过 SharpDX 使用它,尽管这不应影响 DirectX 11 的调试输出。

对应问题: https://github.com/sharpdx/SharpDX/issues/903

DirectX 11 使用 'deferred destruction' 资源,因此通常如果您需要强制销毁,则需要 Flush。例如,在 Direct3D game templates 中,您需要在调整大小之前完全取消绑定并销毁渲染目标:

// Remove any bound render target or depth/stencil buffer
ID3D11RenderTargetView* nullViews [] = { nullptr }; 
m_d3dContext->OMSetRenderTargets(_countof(nullViews), nullViews, nullptr); 

// Destroy the views (which themselves hold the references to the resources)
m_renderTargetView.Reset(); 
m_depthStencilView.Reset(); 

// Flush the immediate context to force cleanup
m_d3dContext->Flush();