0
\$\begingroup\$

I have an instance where a class creates a DX9 texture via

UINT usage = D3DUSAGE_RENDERTARGET;
HRESULT hr = D3DXCreateTexture (m_D3DDevice, 
                                200, 
                                200, 
                                0, 
                                usage, 
                                D3DFMT_X8R8G8B8, 
                                D3DPOOL_DEFAULT, 
                                &m_texture)

Where m_texture is IDirect3DTexture9* and hr = S_OK after creation.

But when I come to cleaning it up in the destructor via

if (m_texture != NULL)
{
    hr = m_texture->Release();
}

hr = S_FALSE

Why would it fail to release a texture?

\$\endgroup\$
4
\$\begingroup\$

Release does not return an HRESULT, it returns a ULONG. That ULONG is the new reference count on the object (after the release).

hr == S_FALSE in your code because Release is returning 1 (which is what S_FALSE is defined as), which means there's one pending reference count to the object after you release it (that's not necessarily a problem, it could be an internal D3D reference).

The return code from Release is intended only for diagnostic purposes. But, tldr, it's not failing, it's working fine.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Hmm, HRESULT on other Release() calls returns S_OK though. and when I comment out m_texture->Release(), I don't get a memory error when the program is closed. \$\endgroup\$ – unknownSPY Apr 20 '16 at 19:09
  • \$\begingroup\$ Release() does not result an HRESULT! Stop casting it to one. It's just returns number, and one you cannot meaningfully rely on. It's perfectly valid for Release() to return 0, 1, 500, 128374, whatever. It simply returns to you the number of outstanding references after release, those references may not have been created by you at all. As long you are pairing every AddRef (or every create) with a Release, you're fine. If you've enabled memory leak tracking in D3D and aren't getting warnings about unreleased objects, you're fine. \$\endgroup\$ – user1430 Apr 20 '16 at 19:14
  • \$\begingroup\$ Save yourself a lot of pain and stop using raw pointers to COM interfaces. Use Microsoft::WRL::ComPtr so you don't have to worry about manual AddRef and Release counts. \$\endgroup\$ – Chuck Walbourn Jul 27 '16 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.