Marshal.ReleaseComObject() vs. Marshal.FinalReleaseComObject()
很简单,不翻译了。
If you are using COM components on your .NET code, you might be already aware of the Marshal.ReleaseComObject and Marshal.FinalReleaseComObject, which are used to release the managed reference to Runtime Callable Wrapper (RCW) of the COM object.
If both are used to release COM objects from memory, and you are unsure which one to use, then this post will help you to learn the differences between the calls
.NET Framework is always a better choice managing your .NET objects. Once you are done, and the object that you are referring to goes out of scope, Garbage Collector automatically handles it to remove from memory. But that's not the same for unmanaged COM objects. Even though the scope of the instance variable goes out of it, but the reference still stays in memory as an RCW (Runtime Callable Wrapper).
Every time a COM interface pointer enters into the Common Language Runtime (CLR), it wraps it inside an RCW. Then the RCW keeps a reference count which is then incremented every time a COM interface pointer is mapped to it.
To release that memory instance, Marshal.ReleaseComObject method is used which enables you to force the RCW and decrements the reference count. When the reference count reaches to zero, the runtime releases all its references on that unmanaged COM object. Attempting to access that object afterwards, throws you an Exception.
Marshal.FinalReleaseComObject also does the same, but with a little difference. When you call Marshal.ReleaseComObject method, it releases one RCW reference and decrements the count by one. To remove all the managed references to a COM object, you need to call the Marshal.ReleaseComObject, the following way, in a loop until it returns '0' (zero) to free up the COM object:
while (Marshal.ReleaseComObject(obj) != 0) ;
But in case of Marshal.FinalReleaseComObject, you don't have to loop in to release all the associated references to RCW. Because, the method itself performs the same internally. If you check the original implementation of the System.Runtime.InteropServices.Marshal class, you will notice the following:
public static int FinalReleaseComObject (object o)
{
while (ReleaseComObject (o) != 0) ;
return 0;
}
So, after reading this, which one will you prefer? It's definitely the FinalReleaseComObject as it internally loops through the RCW to release all the instance references, until the count becomes '0' (zero). Isn't it? Call the ReleaseComObject only if it is absolutely required to perform on a single reference.
To learn further about the above two methods, checkout the MSDN reference: Marshal.ReleaseComObject and Marshal.FinalReleaseComObject. I hope that the above post was clear and easy to understand. Don't forget to read my other posts/articles published on this blog.
其实许多人建议不要使用Marshal这个类,因为可能会发生异常情况。但如果一定要用,看完了上面的洋文,你知道该用哪一个了么?
从这里抄来的:https://www.kunal-chowdhury.com/2017/08/marshal-release-com-object.html
Marshal.ReleaseComObject() vs. Marshal.FinalReleaseComObject()的更多相关文章
- C#双面打印解决方法(打印word\excel\图片)
最近需要按顺序打印word.excel.图片,其中有的需要单面打印,有的双面.网上查了很多方法.主要集中在几个方式解决 1.word的print和excel的printout里设置单双面 2.prin ...
- .net开发Ae释放com对象的问题
本文转载自: http://www.cnblogs.com/yhlx125/archive/2011/11/22/2258543.html#2269154我的博文 http://www.cnblogs ...
- 毫秒级的时间处理上G的图片(生成缩略图)
测试环境: 测试图片(30M): 测试计时方法: Stopwatch sw1 = new Stopwatch(); sw1.Start(); //TODO...... sw1.Stop(); stri ...
- 关闭EXCEL进程
//导入Windows类库,可以获得进程ID [DllImport("User32.dll", CharSet = CharSet.Auto)] pub ...
- c#.net Excel中的数据导入到SQL数据库中
/// <summary> /// 从Excel 导入学生 /// </summary> /// <param name=&qu ...
- 向Word模板中填充数据
现在有这样的需求,给Word文档的指定位置填充上特定数据,例如我们有一个终端,用来打印员工的薪资证明,对于一个公司来说,他的薪资证明模板是固定的,变化的地方是员工姓名,部门,职位等.我们只需要将这些指 ...
- DataTable导出到Excel(.NET 4.0)
最近在论坛里又看到很多关于DataTable(DataSet)导入Excel的帖子,我也温故知新一下,用VS2010重新整理了一个Sample.这个问题简化一下就是内存数据到文件,也就是遍历赋值,只不 ...
- PDF/WORD/EXCEL 图片预览
一.PDF/WORD/EXCEL 转 XPS 转 第一页内容 转 图片 WORD.EXCEL转XPS (Office2010) public bool WordToXPS(string sourceP ...
- PDF转图片 C# with Adobe API
PDF转图片大概有十几种方式,褒贬不一,我就详细给大家说一下我认为效率最高的方式,使用Adobe官方的SDK 安装acrobat reader 9.0以上即可,勾选如下组件.
随机推荐
- MySQL运行计划初探
-Mysql运行计划总结– 1 运行计划概述 先看看一个运行计划 mysql> explain SELECT * FROM EMP , DAO_OBJECTS t1 , DAO_OBJECTS ...
- Changing the Output Path in your Web Applications is a bad idea
http://lnbogen.com/2006/09/20/changing-the-output-path-in-your-web-applications-is-a-bad-idea/ Let’s ...
- 统计ES性能的python脚本
思路:通过http请求获取es集群中某一index的索引docs数目变化来进行ES性能统计 import time from datetime import datetime import urlli ...
- P3959 宝藏 状压dp
之前写了一份此题关于模拟退火的方法,现在来补充一下状压dp的方法. 其实直接在dfs中状压比较好想,而且实现也很简单,但是网上有人说这种方法是错的...并不知道哪错了,但是就不写了,找了一个正解. 正 ...
- bzoj2648
http://www.lydsy.com/JudgeOnline/problem.php?id=2648 kdtree裸题... 抄板子一边抄对了... 挺好理解的,就是说我们先找出中间的元素,然后小 ...
- 37. ext 中sm什么意思
转自:https://zhidao.baidu.com/question/112450217.htmlsm是SelectionModel的缩写默认为RowSelectionModel其他模式还有Che ...
- java网络编程TCP
图片来自网络 [服务端] import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; im ...
- istio-禁用/允许sidecar设置
一.在namespace设置自动注入: 给 default 命名空间设置标签:istio-injection=enabled: $ kubectl label namespace default is ...
- Istio 1.1部署实践
前提条件 正确安装配置Kubernetes集群 CentOS Linux release 7.5.1804 安装 下载istio 1.1版本 [root@vm157 ~]# wget https:// ...
- ArgumentError: You need to supply at least one validatio
创建: 2017/10/02 意思: validate没有内容 例: validates :title