Advertisement Jump to content
Sign in to follow this  
d07RiV

Releasing resources from a D3D hook

This topic is 1796 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a DLL that hooks D3D EndScene call to draw an overlay; to do so, it creates a few textures to avoid re-creating the image on every frame. However, I need to detect when I should release the textures. If I don't do so, I get the "has non-zero reference count" errors from the SDK samples and certain games.

 

I was able to fix errors on resizing by simply hooking Reset; however, that did not fix the errors when the target application is closed. I suppose the only way to fix that would be to hook Release, but then I would not know whether the device is actually being released or its reference count is simply decreased - as my own textures also store references to the device and I have no clean way of counting them. I could try releasing the textures on every call to Release and then re-creating them if needed, but I'm afraid that my hook will be called even when D3D code implicitly calls Release, e.g. when releasing a resource associated with that device. So if the app creates and releases some objects on every frame, this will cause my textures to get destroyed and re-created on every frame, defeating the purpose of storing them.

 

Is there a way to know how many times my objects reference the device, other than simply calling AddRef, creating them, then Release and calculate the difference between return values?

Share this post


Link to post
Share on other sites
Advertisement

Is there a way to know how many times my objects reference the device, other than simply calling AddRef, creating them, then Release and calculate the difference between return values?

No, and using the return value from AddRef/Release is intended for test purposes only - you should not rely on it for production code.

 

In your case, when you hook the device's Release method, you could let the original Release method do it's work, then try to access the device pointer (maybe call QueryInterface, requesting a pointer to IUnknown), wrapped in a try-catch statement, to cach the memory-access violation exception (you have to compile with /EHa http://msdn.microsoft.com/en-us/library/1deeycx5.aspx ). If you catch the exception, then the device was destroyed.

 

Unfortunately, this won't help you, since the device will not be destroyed until your resources are also destroyed?

 

The only way I see is to hook the device's AddRef and Release methods, and keep track of the reference count yourself.

Edited by tonemgub

Share this post


Link to post
Share on other sites

Problem is, the device will never be actually destroyed as long as I'm holding resources that reference it.

Share this post


Link to post
Share on other sites

I do that, but if the game window is closed first, it will complain about the device still being references.

I've found a solution for now - since I already have the CBT hook, I can detect when the window is being destroyed, and release my objects.

 

e: hmm I've run into a new problem, though its kinda unrelated to this thread. A certain game only renders to a certain surface (which is not the backbuffer) and never calls Present (I've checked SwapChain::Present as well). How can it do the rendering in this case? UpdateSurface doesn't seem to be called, either.

Edited by d07RiV

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!