CriticalFinalizerObject Class
Ensures that all finalization code in derived classes is marked as critical.
Assembly: mscorlib (in mscorlib.dll)
Classes deriving from the CriticalFinalizerObject class are implicitly treated as a constrained execution region (CER). This requires code in the finalizer to only call code with a strong reliability contract. For more information about CERs, see the System.Runtime.ConstrainedExecution namespace.
In classes derived from the CriticalFinalizerObject class, the common language runtime (CLR) guarantees that all critical finalization code will be given the opportunity to execute, provided the finalizer follows the rules for a CER, even in situations where the CLR forcibly unloads an application domain or aborts a thread. If a finalizer violates the rules for a CER, it might not successfully execute. In addition, the CLR establishes a weak ordering among normal and critical finalizers: for objects reclaimed by garbage collection at the same time, all the noncritical finalizers are called before any of the critical finalizers. For example, a class such as FileStream, which holds data in the SafeHandle class that is derived from CriticalFinalizerObject, can run a standard finalizer to flush out existing buffered data.
In most cases, you do not need to write classes that derive from the CriticalFinalizerObject class. The .NET Framework class library provides two classes, SafeHandle and CriticalHandle, that provide critical finalization functionality for handle resources. Furthermore, the .NET Framework provides a set of prewritten classes derived from the SafeHandle class, and this set is located in the Microsoft.Win32.SafeHandles namespace. These classes are designed to provide common functionality for supporting file and operating system handles.
The following code example shows the use of the SafeFileHandle class to provide critical finalization for the standard input and output streams. The SafeFileHandle, derived from the SafeHandle class, is passed to the file stream in the FileStream constructor.
Imports System.Runtime.InteropServices Imports System.IO Imports Microsoft.Win32.SafeHandles Public Module Example Const STD_INPUT_HANDLE As Integer = -10 Const STD_OUTPUT_HANDLE As Integer = -11 Const STD_ERROR_HANDLE As Integer = -12 Public Declare Auto Function GetStdHandle Lib "Kernel32" (type As Integer) As IntPtr Public Sub Main() Dim fsIn As FileStream = Nothing Dim fsOut As FileStream = Nothing Try Dim sfhIn As New SafeFileHandle(GetStdHandle(STD_INPUT_HANDLE), False) fsIn = new FileStream(sfhIn, FileAccess.Read) Dim input() As Byte = { 0 } fsIn.Read(input, 0, 1) Dim sfhOut As New SafeFileHandle(GetStdHandle(STD_OUTPUT_HANDLE), False) fsOut = New FileStream(sfhOut, FileAccess.Write) fsOut.Write(input, 0, 1) Dim sf As SafeFileHandle = fsOut.SafeFileHandle Finally If fsIn IsNot Nothing Then fsIn.Close() fsIn = Nothing End If If fsOut IsNot Nothing Then fsOut.Close() fsOut = Nothing End If End Try End Sub End Module
- SecurityPermission
for permission to call unmanaged code. Security action: LinkDemand. Associated enumeration: SecurityPermissionFlag.UnmanagedCode
System.Runtime.ConstrainedExecution.CriticalFinalizerObject
System.Runtime.InteropServices.CriticalHandle
System.Runtime.InteropServices.SafeHandle
System.Runtime.MemoryFailPoint
System.Security.SecureString
System.Threading.ReaderWriterLock
System.Threading.Thread
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.