Данная статья переведена с помощью средств машинного перевода. Чтобы просмотреть ее на английском языке, установите флажок Английский. Вы также можете просматривать английский текст во всплывающем окне, наводя указатель мыши на переведенный текст.
Перевод
Английский

Метод Marshal.ReleaseComObject (Object)

 

Уменьшает счетчик ссылок оболочки Runtime Callable Wrapper (RCW), связанной с указанным COM-объектом.

Пространство имен:   System.Runtime.InteropServices
Сборка:  mscorlib (в mscorlib.dll)

[SecurityCriticalAttribute]
public static int ReleaseComObject(
	object o
)

Параметры

o
Type: System.Object

Освобождаемый COM-объект.

Возвращаемое значение

Type: System.Int32

Новое значение счетчика ссылок оболочки RCW, связанной с o. Это значение обычно равно нулю, поскольку оболочка RCW хранит только одну ссылку на COM-объект в оболочке вне зависимости от количества управляемых клиентов, которые ее вызывают.

Exception Condition
ArgumentException

o не является допустимым COM-объектом.

NullReferenceException

Свойство o имеет значение null.

This method is used to explicitly control the lifetime of a COM object used from managed code. 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.

Every time a COM interface pointer enters the common language runtime (CLR), it is wrapped in an RCW.

The RCW has a reference count that is incremented every time a COM interface pointer is mapped to it. The M:System.Runtime.InteropServices.Marshal.ReleaseComObject(System.Object) method decrements the reference count of an RCW. When the reference count reaches zero, the runtime releases all its references on the unmanaged COM object, and throws a T:System.NullReferenceException if you attempt to use the object further. 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 M:System.Runtime.InteropServices.Marshal.ReleaseComObject(System.Object) returns the number of remaining references.

This method enables you to force an RCW reference count release so that it occurs precisely when you want it to. However, improper use of M:System.Runtime.InteropServices.Marshal.ReleaseComObject(System.Object) may cause your application to fail, or may cause an access violation.

Consider a scenario in which managed code in an application domain is holding onto an RCW that represents a COM component. If you call the M:System.Runtime.InteropServices.Marshal.ReleaseComObject(System.Object) method on the RCW, the managed code will be unable to access the RCW and will raise an T:System.Runtime.InteropServices.InvalidComObjectException exception.

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.

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 CoCreateInstancehttp://go.microsoft.com/fwlink/?LinkID=142894 function, which returns the same interface pointer every time it is called for singleton COM components. 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 M:System.Runtime.InteropServices.Marshal.ReleaseComObject(System.Object) method on the COM component, the other will be broken.

Therefore, use the M:System.Runtime.InteropServices.Marshal.ReleaseComObject(System.Object) only if it is absolutely required. If you want to call this method to ensure that a COM component is released at a determined time, consider using the M:System.Runtime.InteropServices.Marshal.FinalReleaseComObject(System.Object) method instead. M:System.Runtime.InteropServices.Marshal.FinalReleaseComObject(System.Object) will release the underlying COM component regardless of how many times it has re-entered the CLR. The internal reference count of the RCW is incremented by one every time the COM component re-enters the CLR. Therefore, you could call the M:System.Runtime.InteropServices.Marshal.ReleaseComObject(System.Object) method in a loop until the value returned is zero. This achieves the same result as the M:System.Runtime.InteropServices.Marshal.FinalReleaseComObject(System.Object) method.

SecurityCriticalAttribute

requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Универсальная платформа Windows
Доступно с 8
.NET Framework
Доступно с 1.1
Переносимая библиотека классов
Поддерживается в: переносимые платформы .NET
Windows Phone Silverlight
Доступно с 8.0
Windows Phone
Доступно с 8.1
Вернуться в начало
Показ: