简体   繁体   中英

Releasing resources from D3D hook

I have a DLL that hooks D3D calls of another application. In particular, it creates some textures to be used for rendering. However, when the target application is closed before the hook is removed, it sometimes causes the "non-zero reference count" error message to appear. Obviously this is because the textures owned by the hook were not released in time.

I was able to get rid of this error when the window is resized etc by hooking Reset call and releasing the resources; however, I'm not sure what to do when the window is actually released. I could hook the Release method, but I see no clear way of telling whether the device is actually being destroyed or its reference count is simply decremented. The return value of the original Release call doesn't tell me much, because the hook's textures are still holding references to the device, and keeping track of these references seems like too much work.

I could simply always release all my textures when Release is called, and then re-create then when I need them again, but since the Release hook will probably be triggered by implicit release calls from within the D3D code, this might cause too much overhead. Eg if the host application creates some small resource and releases it in every frame, this could cause the hook to discard and re-create all textures on every frame as well, which is rather costly.

Since the author of the topic never came back here, I would like to provide my solution to the problem. Instead of hooking specific functions, implement a proxy D3D class which should keep a reference to the real interface. On destruction, it releases the interface. Now to solve your issue, since you know the real interface could never be released before your class, call the real Release method from within your proxy Release, if the reference count reaches 1, release your resources and destroy the proxy class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM