Disposing of Textures with DebugNames

Started by
2 comments, last by ericrrichards22 10 years, 5 months ago

When creating ShaderResourceViews in my engine, I've started assigning them DebugName properties to assist in debugging, using the VS 2013 graphics debugger. I've noticed that since I've started doing this, the underlying Texture objects aren't getting disposed of correctly, and SlimDX complains of memory leaks when the program closes. The code to create a ShaderResourceView looks like this:


CubeMapSRV = ShaderResourceView.FromFile(device, filename);
CubeMapSRV.Resource.DebugName = "sky cubemap";

The code to dispose of the ShaderResourceView looks like this:


if (CubeMapSRV != null){
    CubeMapSRV.Dispose();
    CubeMapSRV = null;
}

The object creation stack trace printed by SlimDX is indicating that it is the CubeMapSRV.Resource.DebugName assignment that is leaking; specifically, the underlying Texture object. Is assigning the DebugName creating a reference to the Texture somewhere that is hidden from me?

Eric Richards

SlimDX tutorials - http://www.richardssoftware.net/

Twitter - @EricRichards22

Advertisement
Hmmm, one could either consider this a bug in the implementation or a wrong usage of the Resource property. Usually a D3D11 API GetWhatEver functions increases the reference counter (see remarks of ID3D11View::GetResource). This happens when reading the C# property. I don't see a Release/Dispose counterpart here, so you need to do one yourself. Should work fine when doing that immediately after setting the debug name.

Alternatives:
- Use textures explicitly and don't directly create SRVs.
- Use the debug name on the SRV, not the texture

I ran into the same problem a few days ago. As unbird mentions you must dispose of any device resource you acquire a reference to by calling Get* functions. One thing to remember is that Dispose() does not destroy the resource, it simply decreases the reference count and invalidates said reference, the resource itself is only destroyed when it hits zero. So in theory this should work:


using (Resource resource = CubeMapSRV.Resource)
{
    resource.DebugName = "sky cubemap";
}

In practice, I prefer to avoid messing around with reference counters and instead created a helper class which stores a resource and provides non-reference-counted access to SRV's, RTV's, UAV's, ... By the way, this problem also applies to ImmediateContext and a bunch of other stuff, and you don't need to set variables to null after disposing them. I think the underlying issue is that the C# memory management model does not quite map to how DirectX works, so you need to be a bit more careful with what you do to avoid making incorrect assumptions.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Thanks for the explanations. I've ended up going with assigning the DebugName inside a using, as Bacterius suggested. Disposing of the SRV's Resource property also works, but I found it a little more cumbersome.

you don't need to set variables to null after disposing them

Mainly, I am doing this in order to make sure I'm not accessing anything after it's been disposed; the bulk of my SlimDX ComObjects are bound up in my game objects, so for all intents and purposes, when I Dispose of a game object, I want it to return the game object to a completely uninitialized state.

Eric Richards

SlimDX tutorials - http://www.richardssoftware.net/

Twitter - @EricRichards22

This topic is closed to new replies.

Advertisement