Marshal.Release(IntPtr) Método

Definição

Diminui a contagem de referência na interface especificada.

public:
 static int Release(IntPtr pUnk);
[System.Security.SecurityCritical]
public static int Release (IntPtr pUnk);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static int Release (IntPtr pUnk);
public static int Release (IntPtr pUnk);
[<System.Security.SecurityCritical>]
static member Release : nativeint -> int
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member Release : nativeint -> int
static member Release : nativeint -> int
Public Shared Function Release (pUnk As IntPtr) As Integer

Parâmetros

pUnk
IntPtr

nativeint

A interface a ser liberada.

Retornos

O novo valor da contagem de referência na interface especificada pelo parâmetro pUnk.

Atributos

Exemplos

O exemplo a seguir demonstra como recuperar uma IUnknown interface para um objeto gerenciado usando o GetIUnknownForObject método . Em seguida, o exemplo libera o ponteiro da interface chamando o Release método .

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();
    }
}
Imports System.Runtime.InteropServices

Module Program


    Sub Run()

        ' Dim an Integer object
        Dim IntegerObject As Integer = 1

        ' Dim a pointer
        Dim pointer As IntPtr

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

        ' Get the IUnKnown pointer for the Integer object
        pointer = Marshal.GetIUnknownForObject(IntegerObject)

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

        ' Always call Marshal.Release to decrement the reference count.
        Marshal.Release(pointer)



    End Sub

    Sub Main(ByVal args() As String)

        Run()

    End Sub

End Module

Comentários

O Common Language Runtime gerencia a contagem de referência de um objeto COM para você, tornando desnecessário usar esse método diretamente. Use esse valor somente para fins de teste. Em casos raros, como testar um marshaler personalizado, você pode achar necessário manipular o tempo de vida de um objeto manualmente. Somente os programas que chamam Marshal.AddRef devem chamar Release. Chamar Release depois que a contagem de referência atingir zero causa comportamento indefinido.

Você pode chamar Marshal.GetComInterfaceForObject, Marshal.GetIUnknownForObjectou Marshal.GetIDispatchForObject para obter um IntPtr valor que representa um ponteiro de interface IUnknown para lançamento. Você também pode usar esses métodos e o Release método em objetos gerenciados para liberar as interfaces COM representadas pelo Wrapper Callable COM do objeto gerenciado.

Aplica-se a

Confira também