Sign in to follow this  
schupf

Is it save to release resources?

Recommended Posts

Hello, I have a little question about Direct3D10. Is it save to release a resource that has been used to create another resource onto it (for example a view). Example:
 ID3D10Texture2D* dsTex = NULL;
// ...
device->CreateTexture2D(&dsDesc, NULL, &dsTex); // create 2D texture

device->CreateDepthStencilView(dsTex, &dsvDesc, &dsv); // create view onto 2D texture

dsTex->Release(); // Release texture
I create a 2D texture. After the call of CreateTexture2D() the ref count of dsTex is 1. Then I create a DSV with CreateDepthStencilView(). After the call of CreateDepthStencilView() dsv has ref count 1 and dsTex has (surprisingly) still a ref count of 1. Then I release the texture and the texture is destroyed. I use this code and it works, but I want to know if this is just coincidence or if this is always save? I would understand why this code works if the ref count of the 2D texture was 2 after the call of CreateDepthStencilView() (because the DSV references the texture), but its 1. So actually I delete the texture which is referenced by the DSV. Why does this work?

Share this post


Link to post
Share on other sites
First at all. The COM-standard defines that the return values form AddRef and Release are only for diagnostic purpose. They don’t need to represent the real ref count of an object.

Direct3D 10 (and 11) uses two references counters. An internal and an external one. The reason for this could be to prevent an API user to release an object that is still used by another object. I see such things in DX9 code that tends to behave very strange after this.

Therefore it is safe in this case. It’s defined as not safe to release a resource that is assigned to the device as an assignment to the current device pipeline would not change the reference count of an object.

Share this post


Link to post
Share on other sites
Thanks Demirug.

@XVincentX: I wish it was so, but the reality is different. The documention is really inconsistent about telling you when the ref counter is manipulated.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this