This topic has not yet been rated - Rate this topic

Marshal.Release Method

Decrements the reference count on the specified interface.

Namespace:  System.Runtime.InteropServices
Assembly:  mscorlib (in mscorlib.dll)
public static int Release(
	IntPtr pUnk
)

Parameters

pUnk
Type: System.IntPtr
The interface to release.

Return Value

Type: System.Int32
The new value of the reference count on the interface specified by the pUnk parameter.

The common language runtime manages the reference count of a COM object for you, making it unnecessary to use this method directly. Use this value only for testing purposes. In rare cases, such as testing a custom marshaler, you might find it necessary to manipulate an object's lifetime manually. Only programs that call Marshal.AddRef should call Release. Calling Release after the reference count has reached zero causes undefined behavior.

You can call Marshal.GetComInterfaceForObject, Marshal.GetIUnknownForObject, or Marshal.GetIDispatchForObject to obtain an IntPtr value that represents a IUnknown interface pointer to release. You can also use these methods and the Release method on managed objects to release the COM interfaces represented by the managed object's COM Callable Wrapper.

The following example demonstrates how to retrieve an IUnknown interface for a managed object using the GetIUnknownForObject method. The example then releases the interface pointer by calling the Release method.


using System;
using System.Runtime.InteropServices;

class Program
{

    static void Run()
    {

        // Create an int object
        int obj = 1;

        Console.WriteLine("Calling Marshal.GetIUnknownForObject...");

        // Get the IUnKnown pointer for the Integer object
        IntPtr pointer = Marshal.GetIUnknownForObject(obj);

        Console.WriteLine("Calling Marshal.Release...");

        // Always call Marshal.Release to decrement the reference count.
        Marshal.Release(pointer);
    }

    static void Main(string[] args)
    {
        Run();
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
  • SecurityCriticalAttribute  

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

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Marshal.Release needs to be called on IntPtrs of COM Objects
Remarks section of this method is a bit misleading as it can be understood as if you have an RCW of a COM object you don't have to call this method. In fact, almost always you need to call this method if you have an IntPtr of a COM object whether there is an RCW or not. Reason is, unless some low level unsafe hackery is involved, any IntPtr handed to you that represent a COM object is first reference count incremented. For instance, Marshal.GetIUnknownForObject increments the reference count even though there is an RCW for the object that is provided to it so you need to call this method once you're done with IntPtr that it returns. Also some of the COM interop API return raw IntPtr as COM objects instead of actual interface type. So for those IntPtrs you also need to call Marshal.Release because CLR has no idea that they represent COM objects. It would just treat it as a raw void pointer. Even if you use Marshal.GetObjectForIUnknown on IntPtr you've got to create RCW, you still have to call Marshal.Release because original IntPtr has been handed you after its reference count is incremented.