We need you! We're working hard on the next version of Developer Fusion -
Let us know what you think we should be up to!
- using System;
- using System.Reflection;
- using System.Threading;
- using System.Security.Permissions;
- using System.Runtime.CompilerServices;
- using System.Runtime.ConstrainedExecution;
- using System.IO;
- namespace System.Runtime.InteropServices
- {
-
-
-
-
-
-
-
-
- [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
- [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
-
- public abstract class CriticalHandle : CriticalFinalizerObject, IDisposable
- {
-
-
- #if DEBUG
- private string _stackTrace;
-
- #endif
- protected IntPtr handle;
-
- private bool _isClosed;
-
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
- protected CriticalHandle(IntPtr invalidHandleValue)
- {
- handle = invalidHandleValue;
- _isClosed = false;
-
- #if DEBUG
- if (BCLDebug.SafeHandleStackTracesEnabled)
- _stackTrace = Environment.GetStackTrace(null, false);
- else
- _stackTrace = "For a stack trace showing who allocated this CriticalHandle, set SafeHandleStackTraces to 1 and rerun your app.";
- #endif
- }
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- ~CriticalHandle()
- {
- Dispose(false);
- }
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- private void Cleanup()
- {
- if (IsClosed)
- return;
- _isClosed = true;
-
- if (IsInvalid)
- return;
-
-
-
-
- int lastError = Marshal.GetLastWin32Error();
-
- if (!ReleaseHandle())
- FireCustomerDebugProbe();
-
- Marshal.SetLastWin32Error(lastError);
-
- GC.SuppressFinalize(this);
- }
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- private extern void FireCustomerDebugProbe();
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- protected void SetHandle(IntPtr handle)
- {
- this.handle = handle;
- }
-
-
-
- public bool IsClosed {
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- get { return _isClosed; }
- }
-
-
-
-
-
- public abstract bool IsInvalid {
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- get;
- }
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- public void Close()
- {
- Dispose(true);
- }
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- public void Dispose()
- {
- Dispose(true);
- }
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- protected virtual void Dispose(bool disposing)
- {
- Cleanup();
- }
-
-
-
-
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- public void SetHandleAsInvalid()
- {
- _isClosed = true;
- GC.SuppressFinalize(this);
- }
-
-
-
-
-
-
-
-
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- protected abstract bool ReleaseHandle();
- }
-
- }