c++ - Direct3D COM 接口(interface)的发布顺序重要吗?

标签 c++ com direct3d

作为一个例子考虑这个结构

struct Foo
{
    ComPtr<ID3D11Texture2D> back_buffer;
    ComPtr<IDXGISwapChain> swap_chain;
    ComPtr<ID3D11DeviceContext> device_context;
    ComPtr<ID3D11Device> device;
}

此处调用 release() 的顺序与创建这些 COM 对象的顺序相同。它会导致任何问题吗?

最佳答案

一般来说,COM 对象的释放顺序严格来说是无关紧要的。也就是说,Direct3D 在生命周期中不使用严格的 COM 规则。一旦您释放了对 Direct3D 设备的最后一个引用,那么该设备的所有子对象都将无效,无论它们各自的引用计数是多少。

为了更容易进行清理,Direct3D 调试设备中有“对象泄漏检测”功能,因此在释放最终设备实例之前干净地释放所有内容会很有帮助。参见 Direct3D SDK Debug Layer TricksDXGI Debug Device .

请记住,对象到对象的引用也可以使计数保持事件状态,并且 Direct3D 使用“延迟销毁”。因此,“干净退出”需要在您取消绑定(bind)所有对象后进行刷新:

m_d3dContext->ClearState();
m_d3dContext->Flush();
// Release all your objects except the device
// Final release of the device here

You don't have to use such a clear & flush for cleanup with Direct3D 11, but you'll end up with a lot of 'false reports' of live objects by the Debug Layer if you don't Flush after unbinding. With Direct3D 12, you have to ensure the GPU is idle before you start releasing things to get a clean exit.

关于c++ - Direct3D COM 接口(interface)的发布顺序重要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40577247/

相关文章:

.net - 无法使用 REGASM/u 删除 COM [PrimaryInteropAssemblyName] 类型库值

c++ - 如何从 ProgID 或 CLSID 获取类型库(不加载 COM 对象)?

microsoft-metro - 将 C++ AMP 与 Direct2D 结合使用

c++ - 当 SampleDesc.Count > 1 时,Direct3D11 DEBUG MODE 在最​​终 COM 版本上崩溃

C++ 函数中两个类之间的交互

c++ - 为什么设置了中文代码页的 Windows 控制台可以显示 UTF-16 编码的字符?

windows - CoInitializeSecurity 错误

c++ - 如何阅读此运算符声明和实现?

c++ - 通过 "inorder"和 "reverse inorder"方法同时遍历二叉搜索树的多线程方法,比较元素对

Opengl 和 Webgl : sampling from a texture attached to current framebuffer