D3D render targets

Started by
10 comments, last by SiCrane 16 years, 1 month ago
Quote:Original post by Sync Views
ah I see... so ->Release() doesn't actually destroy the surface but just decreases some sort of counter so that when I looped through with (*it)->Release() at the end of the program those ones were not freed?
Exactly. Each COM object (D3D objects are COM objects) has a reference count, which is incremented whenever a new reference to it is made (Or you call AddRef() on it, which is what happens internally), and is decremented when Release() is called. When the reference count reaches zero, the object is destroyed.
Usually, you'll just call Release() once, then set the pointer to null, since you'll normally have a reference count of one for each pointer you hold to the object.

In this case:
D3D creates the backbuffer, so gives it a reference count of 1.
You call GetBackBuffer(), which returns it, and increments the refcount to 2.
You call SetRenderTarget() with your new target, which causes D3D to Release() the pointer it holds to the old render target (the one it created), and that sets the refcount to 1.
You render, etc
You call SetRenderTarget() to set the old render target back, which increments the reference count to 2.

If you don't then Release() your pointer to the old render target, the reference count will keep going up and up each time you run that code, until the reference count on the backbuffer reaches some silly number, which never has a matching number of Release()s.
Advertisement
Also, keep in mind that there are smart pointers available that you can use with DirectX interfaces like CComPtr<>.

This topic is closed to new replies.

Advertisement