How exactly does memory processing (i.e. Release function) work with Direct3D?

I ran into a leak in the Direct3D application and I eventually fixed it, but I think the reason for the leak was due to my misunderstanding of how Direct3D handles its memory and interfaces.

I could not find the final article / tutorial on it (please indicate one if you have one), but from what I have collected, it works as such:

  • Each time you call a method Get, the number of references for the returned object increases. Therefore, if I call GetRenderTarget, the displayed surface will have an increased reference count.
  • A call Releaseto an interface decreases the reference count. These first two points collectively mean: every time you get an interface, release it after you finish with it.
  • When the reference count reaches 0, the instance is deleted.

I'm not quite sure if this is correct, but it seems to work in practice. If someone can clarify / confirm how this works, that would be great.

PS, are there any security measures when releasing interfaces? Calling Releaseany number of times on the back buffer does not seem to do any damage (which is good, but I'm not sure why this is not).

+5
source share
4 answers

Direct3D COM, , 15 . , , COM , , , Windows , Direct3D MS Media Foundation, COM.

COM. , , , .

, , - . COM , , IUnknown. IUnknown AddRef() Release(), AddRef() , Release(), .

(, IFoo ** ppObj), , , , , Release(), .

, CComPtr - (- , ). . " " . , , , , - , . . , , Release() . , AddRef() .

+7

addref/release , COM. : CreateObject() ( CreateTexture, GetRenderTarget, GetBackBuffer ..)) Release(), AddRef() Release().

COM IUnknown::Release() . , : "... Release(), 0, .???? PROFIT!!!!! 111" ! AddRef Direct3D 3rd_party, , - . Release AddRef. Release, , . :

. , , , D3D.

(, CComPtr) , . Release, CComPtr dtor, - .

void get_surface(IDirect3DDevice9 *pDevice)
{
  IDirect3DSurface9 *surf0;
  IDirect3DSurface9 *surf1;
  CComPtr<IDirect3DSurface9> surf2;
  CComPtr<IDirect3DSurface9> surf3;
  CComPtr<IDirect3DSurface9> surf4;

  pDevice->GetRenderTarget( 0, surf0 ); // surface reference counter incremented, you should call Release() for this
  surf1 = surf0; // surface reference count is not incremented, you shouldn't call Release() for this

  pDevice->GetRenderTarget( 0, surf2 ); // surface reference counter incremented
  CComPtr<IDirect3DSurface9> surf3 = surf0; // surface reference counter incremented

  surf0->Release(); // release for pDevice->GetRenderTarget( 0, surf0 );
  surf2.Release();  // .Release() used not ->Release() - it is important
  surf4.Release();  // nothing happens because surf4 == 0
} // surf3.Release() is called in surf3 destructor

#define D3D_DEBUG_INFO 3D- d3d runtime. d3d.

CComPtr .

+4

D3D COM-, . ( . Component Object Model MSDN )

AddRef/Release, .

, Get, , IUnknown, AddRef , , Release .

, ( ), / AddRef, , ( Release ).

0 Release, , , . Release . , ( ), , , .

+2

, . , , , , . - shared_ptr, (++ 11) unique_ptr Release(). Direct3D , . ATL CComPtr COM.

+1

All Articles