清理 directx,正确释放 com 引用
我刚刚学习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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
COM 对象采用引用计数。当您调用
Release()
时,您实际上是在减少内部引用计数。如果另一个对象仍然拥有引用,则它不会被销毁。如果应用程序的其他部分最终获得了对 COM 对象的新引用,请确保在完成后调用
Release()
。仔细阅读返回引用的方法的文档,以了解是否必须调用Release()
。通常你必须这么做。例如在 IDirect3DTexture9::GetSurfaceLevel文档:
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 callRelease()
or not. Usually you will have to.For instance in IDirect3DTexture9::GetSurfaceLevel documentation: