This documentation is archived and is not being maintained.

SafeHandleZeroOrMinusOneIsInvalid Constructor

Note: This constructor is new in the .NET Framework version 2.0.

Initializes a new instance of the SafeHandleZeroOrMinusOneIsInvalid class, specifying whether the handle is to be reliably released.

Namespace: Microsoft.Win32.SafeHandles
Assembly: mscorlib (in mscorlib.dll)

protected SafeHandleZeroOrMinusOneIsInvalid (
	bool ownsHandle
protected SafeHandleZeroOrMinusOneIsInvalid (
	boolean ownsHandle
protected function SafeHandleZeroOrMinusOneIsInvalid (
	ownsHandle : boolean



true to reliably release the handle during the finalization phase; false to prevent reliable release (not recommended).

The following code example demonstrates how to create a class that derives from the SafeHandleZeroOrMinusOneIsInvalid class. This example creates a class that wraps a pointer to unmanaged memory.

using System;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

namespace SafeHandleExamples
    class Example
        public static void Main()

            IntPtr ptr = Marshal.AllocHGlobal(10);

            Console.WriteLine("Ten bytes of unmanaged memory allocated.");

            SafeUnmanagedMemoryHandle memHabdle = new SafeUnmanagedMemoryHandle(ptr, true);

            if (memHabdle.IsInvalid)
                Console.WriteLine("SafeUnmanagedMemoryHandle is invalid!.");
                Console.WriteLine("SafeUnmanagedMemoryHandle class initialized to unmanaged memory.");


    // Demand unmanaged code permission to use this class.
    [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
    sealed class SafeUnmanagedMemoryHandle : SafeHandleZeroOrMinusOneIsInvalid

        // Set ownsHandle to true for the default constructor.
        internal SafeUnmanagedMemoryHandle() : base(true) { }

        // Set the handle and set ownsHandle to true.
        internal SafeUnmanagedMemoryHandle(IntPtr preexistingHandle, bool ownsHandle)
            : base(ownsHandle)

        // Perform any specific actions to release the 
        // handle in the ReleaseHandle method.
        // Often, you need to use Pinvoke to make
        // a call into the Win32 API to release the 
        // handle. In this case, however, we can use
        // the Marshal class to release the unmanaged
        // memory.
        override protected bool ReleaseHandle()
            // "handle" is the internal
            // value for the IntPtr handle.

            // If the handle was set,
            // free it. Return success.
            if (handle != IntPtr.Zero)

                // Free the handle.

                // Set the handle to zero.
                handle = IntPtr.Zero;

                // Return success.
                return true;


            // Return false. 
            return false;


  • SecurityPermission  for permission to call unmanaged code. Security action: LinkDemand. Associated enumeration: UnmanagedCode.
  • SecurityPermission  for permission to call unmanaged code. Security action: InheritanceDemand. Associated enumeration: UnmanagedCode.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0