Marshal.Release(IntPtr) Método

Definição

Diminui a contagem de referências na interface especificada.

public:
 static int Release(IntPtr pUnk);
[System.Security.SecurityCritical]
public static int Release(IntPtr pUnk);
public static int Release(IntPtr pUnk);
[<System.Security.SecurityCritical>]
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 para lançar.

Devoluções

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

Atributos

Exemplos

O exemplo seguinte demonstra como recuperar uma IUnknown interface para um objeto gerido usando o GetIUnknownForObject método. O exemplo liberta então o ponteiro da interface ao chamar 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

Observações

O runtime da linguagem comum gere a contagem de referências de um objeto COM por si, tornando desnecessário usar este método diretamente. Use este valor apenas para fins de teste. Em casos raros, como ao testar um marshaler personalizado, pode ser necessário manipular manualmente a vida útil de um objeto. Só os programas que ligam Marshal.AddRef devem ligar Release. Chamar Release depois de a contagem de referências atingir zero causa comportamento indefinido.

Pode chamar Marshal.GetComInterfaceForObject, Marshal.GetIUnknownForObject, ou Marshal.GetIDispatchForObject para obter um IntPtr valor que represente um ponteiro de interface IUnknown para release. Também pode usar estes métodos e o Release método em objetos geridos para libertar as interfaces COM representadas pelo COM Callable Wrapper do objeto gerido.

Aplica-se a

Ver também