清理 directx,正确释放 com 引用

发布于 2024-11-15 00:30:45 字数 1527 浏览 0 评论 0原文

我刚刚学习directx。人们通常通过函数调用创建一个 directx 设备和几个其他 COM 接口,例如

ID3D11CreateDeviceAndSwapChain(.... ,& device, ...);

在我见过的所有教程代码中,com 接口是使用类似于

if (pointer_to_com_object) {
    pointer_to_com_object->Release(); 
    pointer_to_com_object = 0;
}

例如以下内容摘自 Microsoft 的 directx 11 教程中的Tutorial07_2008。

if( g_pSamplerLinear ) g_pSamplerLinear->Release();
if( g_pTextureRV ) g_pTextureRV->Release();
if( g_pCBNeverChanges ) g_pCBNeverChanges->Release();
if( g_pCBChangeOnResize ) g_pCBChangeOnResize->Release();
if( g_pCBChangesEveryFrame ) g_pCBChangesEveryFrame->Release();
if( g_pVertexBuffer ) g_pVertexBuffer->Release();
if( g_pIndexBuffer ) g_pIndexBuffer->Release();
if( g_pVertexLayout ) g_pVertexLayout->Release();
if( g_pVertexShader ) g_pVertexShader->Release();
if( g_pPixelShader ) g_pPixelShader->Release();
if( g_pDepthStencil ) g_pDepthStencil->Release();
if( g_pDepthStencilView ) g_pDepthStencilView->Release();
if( g_pRenderTargetView ) g_pRenderTargetView->Release();
if( g_pSwapChain ) g_pSwapChain->Release();
if( g_pImmediateContext ) g_pImmediateContext->Release();
if( g_pd3dDevice ) g_pd3dDevice->Release();

正如这个问题的答案中指出的那样,COM对象可以引用它们不属于你。

那么,上面的方法是否是清理 directx 的糟糕方法,或者 directx COM 对象永远不会引用您未创建的对象?

我只是问,因为在我学习的过程中,我一直看到这样的做法。

I am just learning directx. One typically creates a directx device and several other COM interfaces through a functions calls such as

ID3D11CreateDeviceAndSwapChain(.... ,& device, ...);

In all tutorial code I have seen, the com interfaces are released using something akin to

if (pointer_to_com_object) {
    pointer_to_com_object->Release(); 
    pointer_to_com_object = 0;
}

e.g. the following is taken from Tutorial07_2008 in the directx 11 tutorials from Microsoft.

if( g_pSamplerLinear ) g_pSamplerLinear->Release();
if( g_pTextureRV ) g_pTextureRV->Release();
if( g_pCBNeverChanges ) g_pCBNeverChanges->Release();
if( g_pCBChangeOnResize ) g_pCBChangeOnResize->Release();
if( g_pCBChangesEveryFrame ) g_pCBChangesEveryFrame->Release();
if( g_pVertexBuffer ) g_pVertexBuffer->Release();
if( g_pIndexBuffer ) g_pIndexBuffer->Release();
if( g_pVertexLayout ) g_pVertexLayout->Release();
if( g_pVertexShader ) g_pVertexShader->Release();
if( g_pPixelShader ) g_pPixelShader->Release();
if( g_pDepthStencil ) g_pDepthStencil->Release();
if( g_pDepthStencilView ) g_pDepthStencilView->Release();
if( g_pRenderTargetView ) g_pRenderTargetView->Release();
if( g_pSwapChain ) g_pSwapChain->Release();
if( g_pImmediateContext ) g_pImmediateContext->Release();
if( g_pd3dDevice ) g_pd3dDevice->Release();

As pointed out in answers to this question COM objects can have references to them you don't own.

So is the above a bad way to clean up directx or do directx COM objects never have references to them you didn't create?

I just ask because I keep seeing it done this way as I am learning.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛清羽墨安 2024-11-22 00:30:45

COM 对象采用引用计数。当您调用 Release() 时,您实际上是在减少内部引用计数。如果另一个对象仍然拥有引用,则它不会被销毁。

如果应用程序的其他部分最终获得了对 COM 对象的新引用,请确保在完成后调用 Release()。仔细阅读返回引用的方法的文档,以了解是否必须调用 Release() 。通常你必须这么做。

例如在 IDirect3DTexture9::GetSurfaceLevel文档:

调用该方法会增加
内部引用计数
IDirect3DSurface9 接口。失败
调用 IUnknown::Release 时
使用完这个 IDirect3DSurface9
接口导致内存泄漏。

COM objects are reference counted. When you call Release(), you are effectively decrementing the internal reference count. If another object still owns a reference, it will not get destroyed.

If some other part of the application end up having a new reference to a COM object, make sure you call Release() when you are done with it. Carefully read documentation of methods that return references to know if you have to call Release() or not. Usually you will have to.

For instance in IDirect3DTexture9::GetSurfaceLevel documentation:

Calling this method will increase the
internal reference count on the
IDirect3DSurface9 interface. Failure
to call IUnknown::Release when
finished using this IDirect3DSurface9
interface results in a memory leak.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文