Jump to content

  • Log In with Google      Sign In   
  • Create Account


Releasing resources from a D3D hook


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 d07RiV   Members   -  Reputation: 115

Like
0Likes
Like

Posted 17 February 2014 - 04:28 AM

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?



Sponsor:

#2 tonemgub   Members   -  Reputation: 605

Like
0Likes
Like

Posted 17 February 2014 - 05:26 AM


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, 17 February 2014 - 05:32 AM.


#3 d07RiV   Members   -  Reputation: 115

Like
0Likes
Like

Posted 17 February 2014 - 05:27 AM

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



#4 MJP   Moderators   -  Reputation: 10059

Like
0Likes
Like

Posted 17 February 2014 - 01:37 PM

Can't you just release your textures when your DLL is unloaded?



#5 d07RiV   Members   -  Reputation: 115

Like
0Likes
Like

Posted 17 February 2014 - 03:36 PM

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, 18 February 2014 - 10:36 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS