发布Excel COM对象 [英] Releasing Excel COM objects

查看:72
本文介绍了发布Excel COM对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些使用Excel COM对象的Excel自动化代码.在其他SO帖子中,我知道一个人应该做以下事情尽早释放对象:

  Excel.Range rng = GetSomeRange();//用rng做某事...元帅.ReleaseComObject(rng); 

但是,如果我遍历某个范围内的单元格,是否遵循以下正确步骤?

  Excel.Range rng = GetSomeRange();foreach(Excel.Range r in rng){//用r做某事元帅.ReleaseComObject(r);//尽早释放}元帅.ReleaseComObject(rng); 

我不确定的是,如果我在 rng 中释放每个 r ,然后我也释放 rng ,我是否有效地释放了 rng 两次还是正确释放对 rng rng 本身的附加引用 r ?

非常感谢!

编辑

我选择了后一种策略:

  Excel.Range rng = GetSomeRange();foreach(Excel.Range r in rng){//用r做某事元帅.ReleaseComObject(r);//尽早释放}元帅.ReleaseComObject(rng); 

这大大减少了内存...

再次-谢谢大家!

解决方案

不幸的是,周围散布着很多错误信息.首先让我清楚地说明答案:

在大多数情况下,您不必致电 Marshal.ReleaseComObject .为什么?因为垃圾收集器会为您做到这一点.*请注意,我说的是,您可能会发现抓住很多参考文献会导致问题(无论出于何种原因).仅在那些情况下,您应该决定调用 Marshal.ReleaseComObject .

参考:

Kristofer,我们一直都有COM对象的自动发布,GC确定需要清理它们.他们没有被清理立即进行,但在GC或二.我已经与团队确认.

他们说:

对于博客中的应用程序类型,这并不重要,如果人们搞砸了他们的应用程序将失败,并且他们将获得明显的引用计数错误.如果将他们的代码加载到办公室内部,而不是相反,这更令人担忧,因为您最终可以发布别人的参考,然后可能会有他们在代码中没有注意到但会破坏其他人的问题而不是Office加载项.

因此,重点是,尽管ReleaseComObject更具确定性,通常没有必要,因为GC会做同样的事情.

此外,我建议您阅读

现在,也许由于某些原因需要确定性地释放那些COM对象,但是在大多数情况下,您可以删除所有这些 Marshal.ReleaseComObject 调用.

I have some Excel automation code that uses Excel COM objects. From other SO posts I know that one should do the following to release objects at the earliest convenience:

Excel.Range rng = GetSomeRange();

// do something with rng...

Marshal.ReleaseComObject(rng);

However, if I loop through cells in a range, is the following the correct procedure?

Excel.Range rng = GetSomeRange();
foreach (Excel.Range r in rng)
{
    // do something with r
    Marshal.ReleaseComObject(r); // release at earliest possible convenience
}

Marshal.ReleaseComObject(rng);

What I'm unsure of here is that if I release each r in rng and then I also release rng am I effectively releasing rng twice or correctly releasing additional references r to rng and rng itself ?

Thanks in adv.!

EDIT

I went for the latter strategy:

Excel.Range rng = GetSomeRange();
foreach (Excel.Range r in rng)
{
    // do something with r
    Marshal.ReleaseComObject(r); // release at earliest possible convenience
}

Marshal.ReleaseComObject(rng);

which has reduced the memory significantly...

again - thanks to all!

解决方案

Unfortunately, there is a lot of false information floating around. First let me state the answer clearly:

You do not have to call Marshal.ReleaseComObject in most cases. Why? Because the Garbage Collector will do it for you. * Note that I said most, you may find that holding onto to many references causes problems (for whatever reason). In those cases only, should you decide to call Marshal.ReleaseComObject.

Reference:

Kristofer, we’ve always had automatic releases for COM objects as the GC determines it needs to clean them up. They don’t get cleaned up immediately, but do get cleaned up at some point later after a GC or two. I've confirmed with this with the team.

They said:

In the type of app in the blog it doesn’t really matter, if people screw up their app will fail and they’ll get obvious ref-counting bugs. In cases where their code is being loaded inside of office, rather than the other way around, it is much more concerning because you can end up releasing someone else’s reference then there can be problems that they don’t notice in their code but breaks someone elses office add-in instead.

So, the point is that while ReleaseComObject is more deterministic, it's usually not necessary as the GC will do the same thing.

Also, I suggest you read Marshal.ReleaseComObject Considered Dangerous.

If you’re tempted to call "Marshal.ReleaseComObject", can you be 100% certain that no other managed code still has access to the RCW? If the answer is ‘no’, then don’t call it. The safest (and sanest) advice is to avoid Marshal.ReleaseComObject entirely in a system where components can be re-used and versioned over time.

Now, maybe there is some reason you need to deterministically release those COM objects, but in most cases you can remove all those Marshal.ReleaseComObject calls.

这篇关于发布Excel COM对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆