How would I delete a texture created with CreateDDSTextureFromFile?

Sep 25, 2013 at 5:01 PM
Hi guys,

I'm creating a WM8 Native App and can load in textures fine with CreateDDSTextureFromFile(). However I have to admit I'm not sure how to remove them and free up all memory used.

I see there is ReleaseAndGetAddressOf() but that does not seem to delete everything as my test still runs out of memory.

Any help would be enormously appreciated :D

Kind regards,
Sep 25, 2013 at 6:44 PM
Edited Sep 25, 2013 at 7:08 PM
Direct3D uses Reference Counting to manage object lifetimes, although unlike true COM behavior, deleting the Direct3D Device invalidates all the DeviceChild objects no matter what their current reference counts. See Managing the Lifetime of an Object

So typically you'd use a WRL::ComPtr to "own" the instance of a texture and then when it goes out of scope, it will automatically release it's count. This is the primary benefit of using a smart-pointer is that you don't need to remember to manually call "AddRef" or "Release". The object itself won't really get deleted until you have released ALL references to it. In the case of a Texture, you will also need to release any references you have to any Shader Resource View for that Texture. This is why, by the way, you can pass nullptr for the texture and just take the textureView back from CreateDDSTextureFromFile: textureView has a reference to texture, so they will both get cleaned up when you release the textureView.

While generally Direct3D 11 uses 'weak references' for set operations, you will find it is useful to fully unbind existing objects from the pipeline to get a 'clean' exit. Calling ID3D11DeviceContext::ClearState() on the immediate context just before releasing it is a good way to do this.

You use the Direct3D DEBUG device to help track down leaks. See this post.
Marked as answer by walbourn on 12/23/2013 at 11:09 PM
Sep 25, 2013 at 8:02 PM
Walbourn thank you very much for the reply - very clear and extremely helpful.

I followed your guidance and the case is solved.

Much appreciate you taking the time and effort to make it so clear.

Kind regards,