简体   繁体   English

从D3D挂钩释放资源

[英]Releasing resources from D3D hook

I have a DLL that hooks D3D calls of another application. 我有一个DLL挂钩了另一个应用程序的D3D调用。 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; 当我通过挂起Reset调用并释放资源来调整窗口大小时,我能够摆脱此错误; 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. 我可以钩上Release方法,但是我看不出明确的方法来判断设备实际上是在被销毁还是其引用计数只是减少了。 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. 原始Release调用的返回值告诉我的并不多,因为该钩子的纹理仍在保留对该设备的引用,并且跟踪这些引用似乎工作量太大。

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. 我可以总是总是在调用Release时释放所有纹理,然后在再次需要它们时重新创建它们,但是由于Release钩子可能会由D3D代码中的隐式release调用触发,因此可能会导致过多的开销。 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. 代替挂钩特定功能,实现代理D3D类,该类应保留对真实接口的引用。 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. 现在解决您的问题,因为您知道真正的接口永远不会在您的类之前发布,所以请从代理Release中调用真正的Release方法,如果引用计数达到1,请释放资源并销毁代理类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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