When to use ReleaseComObject vs FinalReleaseComObject?(何时使用 ReleaseComObject 与 FinalReleaseComObject?)
问题描述
什么时候应该使用 Marshal.FinalReleaseComObject 与
Marshal.ReleaseComObject
?
When should I use Marshal.FinalReleaseComObject
vs Marshal.ReleaseComObject
?
使用Marshal.FinalReleaseComObject
有什么危险吗?
推荐答案
FinalReleaseComObject
有一个优点,它会让你的程序更快地崩溃.无法使用已与其基础 RCW 分离的 COM 对象"是 CLR 告诉您您自己处理 COM 引用计数而不是将其保留 CLR 是一个错误.您的里程可能会有所不同,当它在您的开发机器上工作时,您不能真正相信它会正确.确保在将代码部署到客户的机器时实现良好的错误报告.
There's some virtue in FinalReleaseComObject
, it will crash your program quicker. "COM object that has been separated from its underlying RCW cannot be used" is the CLR telling you that you taking care of COM reference counts yourself instead of leaving it up the CLR was a mistake. Your mileage may vary, you cannot really trust to get it right when it works on your dev machine. Make sure you implement good error reporting when you deploy the code to your customer's machine.
优点是您的代码中只有一个地方出错,即 FinalReleaseComObject
调用.当您使用 ReleaseComObject
时,它会变得更加模糊.因为这会在一段时间内未被检测到,当 CLR 调用最终的 ReleaseComObject
调用相去甚远.但这就是世界末日的情景,更有可能的结果是,由于您错过了艰难的电话,因此电话没有任何区别.像 mumble["foo"]
,一个很难看到被使用的索引器引用.
The virtue is that there's only one place in your code where you got it wrong, the FinalReleaseComObject
call. It gets much fuzzier when you use ReleaseComObject
. Because that will go undetected for a while, crashing your program when the CLR calls the final IUnknown::Release()
, the one that destroys the object. Very far removed from an incorrect ReleaseComObject
call. But that's the doomsday scenario, the more likely outcome is that the call just doesn't make any difference because you missed the hard ones. Like mumble["foo"]
, an indexer reference that is so very hard to see being used.
好吧,我的建议很明显:不要这样做.您正在与一台从出错的机器竞争.这样做只是有点慢.很不错的真实生活报道"是 可在此处获得.沉默的刺客"部分最相关.
Well, my advice is obvious: don't do this. You are competing with a machine that never gets it wrong. It is merely a bit slow at doing so. A very good "report from real life" is available here. The "silent assassin" section is most relevant.
如果让 COM 服务器立即退出是绝对必要的,那么让机器负责将所有引用计数变为 0.您可以使用 GC.Collect() 来完成.但请记住,如果您希望它在调试时也能正常工作,则必须正确放置该调用.它不适用于使用 COM 对象的相同方法,this answer 中对此进行了说明.而是放在调用方法中.
If it is absolutely essential to get a COM server to exit instantly then let the machine take care of getting all the reference counts to 0. You do so with GC.Collect(). But do keep in mind that you have to place that call correctly if you want this to also work when you debug. It won't work in the same method that uses the COM objects, explained in this answer. Put it in the calling method instead.
这篇关于何时使用 ReleaseComObject 与 FinalReleaseComObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:何时使用 ReleaseComObject 与 FinalReleaseComObject?
基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30