Marshal.ReleaseComObject(Object) 方法

定义

递减与指定的 COM 对象关联的运行时可调用包装器 (RCW) 的引用计数。Decrements the reference count of the Runtime Callable Wrapper (RCW) associated with the specified COM object.

public:
 static int ReleaseComObject(System::Object ^ o);
[System.Security.SecurityCritical]
public static int ReleaseComObject (object o);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static int ReleaseComObject (object o);
public static int ReleaseComObject (object o);
[<System.Security.SecurityCritical>]
static member ReleaseComObject : obj -> int
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member ReleaseComObject : obj -> int
static member ReleaseComObject : obj -> int
Public Shared Function ReleaseComObject (o As Object) As Integer

参数

o
Object

要释放的 COM 对象。The COM object to release.

返回

Int32

o 关联的 RCW 的新引用计数值。The new value of the reference count of the RCW associated with o. 此值通常为零,因为无论调用包装 COM 对象的托管客户端有多少,RCW 仅保留对该对象的一次引用。This value is typically zero since the RCW keeps just one reference to the wrapped COM object regardless of the number of managed clients calling it.

属性

例外

o 不是有效的 COM 对象。o is not a valid COM object.

onullo is null.

注解

此方法用于显式控制从托管代码使用的 COM 对象的生存期。This method is used to explicitly control the lifetime of a COM object used from managed code. 你应使用此方法免费释放包含对资源的引用的基础 COM 对象,或在对象必须按特定顺序释放时使用此方法。You should use this method to free the underlying COM object that holds references to resources in a timely manner or when objects must be freed in a specific order.

每当 COM 接口指针进入公共语言运行时 (CLR) 时,它都会包装在 RCW 中。Every time a COM interface pointer enters the common language runtime (CLR), it is wrapped in an RCW.

RCW 有一个引用计数,每次将 COM 接口指针映射到该引用计数时,它都会递增。The RCW has a reference count that is incremented every time a COM interface pointer is mapped to it. ReleaseComObject方法递减 RCW 的引用计数。The ReleaseComObject method decrements the reference count of an RCW. 当引用计数达到零时,运行时将释放其对非托管 COM 对象的所有引用, System.NullReferenceException 如果尝试进一步使用对象,则会引发。When the reference count reaches zero, the runtime releases all its references on the unmanaged COM object, and throws a System.NullReferenceException if you attempt to use the object further. 如果从非托管代码向托管代码传递了同一 COM 接口,则包装上的引用计数将每次递增,并且调用将 ReleaseComObject 返回剩余引用的数目。If the same COM interface is passed more than one time from unmanaged to managed code, the reference count on the wrapper is incremented every time, and calling ReleaseComObject returns the number of remaining references.

此方法使你可以强制 RCW 引用计数版本,使其在你需要时精确地进行。This method enables you to force an RCW reference count release so that it occurs precisely when you want it to. 但是,不当使用 ReleaseComObject 可能会导致应用程序失败,或者可能导致访问冲突。However, improper use of ReleaseComObject may cause your application to fail, or may cause an access violation.

假设应用程序域中的托管代码持有的应用程序域中的托管代码包含在表示 COM 组件的 RCW 上。Consider a scenario in which managed code in an application domain is holding onto an RCW that represents a COM component. 如果对 RCW 调用 ReleaseComObject 方法,托管代码将无法访问 rcw,并将引发 InvalidComObjectException 异常。If you call the ReleaseComObject method on the RCW, the managed code will be unable to access the RCW and will raise an InvalidComObjectException exception.

如果在释放 RCW 时执行对 RCW 的调用,则可能发生更严重的错误。A more serious error may occur if a call to the RCW is executing when the RCW is released. 在这种情况下,发出调用的线程很有可能会导致访问冲突。In this case, there is a good chance that the thread making the call will cause an access violation. 但是,进程内存可能会损坏,进程可能会继续运行,直到由于很难调试的原因而失败。However, process memory may become corrupted, and the process may continue to run until it fails for reasons that are very difficult to debug.

如果正在使用的 COM 组件是单一实例,则此风险更加复杂,原因如下: CLR 通过调用 COM CoCreateInstance 函数来激活 com 组件,在每次为单独 COM 组件调用该函数时,它都会返回相同的接口指针。This risk is compounded when the COM component that is being used is a singleton, for the following reason: The CLR activates COM components by calling the COM CoCreateInstance function, which returns the same interface pointer every time it is called for singleton COM components. 因此,应用程序域中的单独的和独立的托管代码段可以对单独 COM 组件使用同一个 RCW,如果其中任何一个调用 ReleaseComObject com 组件上的方法,另一个将被破坏。Thus, separate and independent pieces of managed code in an application domain can be using the same RCW for a singleton COM component, and if either one calls the ReleaseComObject method on the COM component, the other will be broken.

因此, ReleaseComObject 仅当绝对需要时才使用。Therefore, use the ReleaseComObject only if it is absolutely required. 如果要调用此方法以确保在确定的时间释放 COM 组件,请考虑 FinalReleaseComObject 改用方法。If you want to call this method to ensure that a COM component is released at a determined time, consider using the FinalReleaseComObject method instead. FinalReleaseComObject 将释放基础 COM 组件,而不考虑它重新进入 CLR 的次数。FinalReleaseComObject will release the underlying COM component regardless of how many times it has re-entered the CLR. 每当 COM 组件重新进入 CLR 时,RCW 的内部引用计数将递增1。The internal reference count of the RCW is incremented by one every time the COM component re-enters the CLR. 因此,可以 ReleaseComObject 在循环中调用方法,直到返回值为零。Therefore, you could call the ReleaseComObject method in a loop until the value returned is zero. 这样就实现了与方法相同的结果 FinalReleaseComObjectThis achieves the same result as the FinalReleaseComObject method.

适用于

另请参阅