简体   繁体   English

VB.NET跟踪COM RCW错误

[英]VB.NET Track COM RCW error

I have a really big project that I can not easily strip down. 我有一个非常大的项目,我不能轻易剥离。

When the application is being closed, I get the error 当应用程序关闭时,出现错误

"InvalidComObjectException: A COM object that has been disconnected from the RCW can not be used."

Details: 细节:

System.Runtime.InteropServices.InvalidComObjectException has occured.
  HResult=-2146233049
  Message=A COM object that has been disconnected from its RCW can not be used.
  Source=mscorlib
  StackTrace:
       at System.StubHelpers.StubHelpers.StubRegisterRCW(Object pThis)
  InnerException: 

Unfortunately I can not see what COM object this is about. 不幸的是,我看不到这是什么COM对象。 Does anybody know how I can find that out? 有人知道我怎么能找到答案吗? Unfortunately I can't read ASM to analyze the disassembly. 不幸的是,我无法阅读ASM来分析反汇编。

There are some steps you could try and since you haven't posted any code I will try to enumerate some of the most common key factors... 您可以尝试一些步骤,由于您尚未发布任何代码,因此我将尝试列举一些最常见的关键因素...

First, there is no order when disposing objects. 首先,处理对象时没有顺序。 If a close/dispose/finalize action is invoked, object C might be disposed before object A and if some object is still alive it might try to access an already disposed object. 如果调用close / dispose / finalize动作,则对象C可能会在对象A之前被处置,并且如果某个对象仍然存在,则它可能会尝试访问已处置的对象。

Second, beware of events. 第二,提防事件。 It's very common to get errors about accessing a disposed object originated from an event call. 在访问源自事件调用的已处置对象时会出现错误,这很常见。

Third, do not dispose objects inside an event scope nor destructors. 第三,不要在事件作用域或析构函数中放置对象。 Create your own method to free your object(s). 创建您自己的方法以释放您的对象。

Since you don't know which COM object is the culprit, I suggest you look for the ones that you do close, dispose, disconnect, etc... 由于您不知道哪个COM对象是罪魁祸首,因此建议您查找那些确实要关闭,处置,断开连接的对象...

You might want to read this blog in order to better understand how RCW's work and also to help you with your problem. 您可能需要阅读此博客 ,以便更好地了解RCW的工作方式,并帮助您解决问题。

Edit : 编辑

After reading your comment, I felt I should add two possible causes: 阅读您的评论后,我觉得我应该添加两个可能的原因:

  • If after removing Microsoft.VisualBasic runtime methods you've solved your problem, then I suspect that inside one, or more, of those methods you have incremented the count of a reference to one, or more, of your COM(s) and did not release properly. 如果在删除Microsoft.VisualBasic runtime methods您解决了问题,那么我怀疑在一个或多个方法中,您已将对一个或多个COM的引用计数增加了,并且释放不正确。 Be sure to ReleaseComObject checking the reference count until it's 0 and then set it to nothing (VB) or null (C#) and let the Garbage Collector do the rest. 确保ReleaseComObject检查引用计数直到其为0,然后将其设置为none (VB)或null (C#),然后由垃圾收集器完成其余工作。

  • The other option is that one of those methods tried to access an already disposed reference to a COM object, resulting in a RCW error, since is no longer callable. 另一个选择是这些方法之一试图访问已经放置的对COM对象的引用,导致RCW错误,因为它不再可调用。

As a final comment, after releasing the COM object always set it to nothing or null , this will release the reference to the variable and you can always check it's availability anywhere in the code. 最后,释放COM对象后,始终将其设置为nonenull ,这将释放对变量的引用,您始终可以在代码中的任何位置检查其可用性。

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

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