internal void Dispose(bool disposing) { if (_dc != null) { DbgUtil.AssertFinalization(this, disposing); try { // Restore original dc. _dc.RestoreHdc(); if (_disposeDc) { _dc.Dispose(disposing); } if (_graphics != null) // if created from a Graphics object... { _graphics.ReleaseHdcInternal(_dc.Hdc); _graphics = null; } } catch (Exception ex) when(!ClientUtils.IsSecurityOrCriticalException(ex)) { Debug.Fail("Exception thrown during disposing: \r\n" + ex.ToString()); } finally { _dc = null; } } }
internal void Dispose(bool disposing) { if (_disposed) { return; } Disposing?.Invoke(this, EventArgs.Empty); _disposed = true; switch (_dcType) { case DeviceContextType.Information: case DeviceContextType.NamedDevice: Interop.Gdi32.DeleteDC(new HandleRef(this, _hDC)); _hDC = IntPtr.Zero; break; case DeviceContextType.Memory: Interop.Gdi32.DeleteDC(new HandleRef(this, _hDC)); _hDC = IntPtr.Zero; break; // case DeviceContextType.Metafile: - not yet supported. case DeviceContextType.Unknown: default: return; // do nothing, the hdc is not owned by this object. // in this case it is ok if disposed throught finalization. } DbgUtil.AssertFinalization(this, disposing); }
/// <include file='doc\IDeviceContext.uex' path='docs/doc[@for="DeviceContext.Dispose1"]/*' /> internal void Dispose(bool disposing) { if (_disposed) { return; } if (Disposing != null) { Disposing(this, EventArgs.Empty); } _disposed = true; switch (_dcType) { case DeviceContextType.Display: Debug.Assert(disposing, "WARNING: Finalizing a Display DeviceContext.\r\nReleaseDC may fail when not called from the same thread GetDC was called from."); ((IDeviceContext)this).ReleaseHdc(); break; case DeviceContextType.Information: case DeviceContextType.NamedDevice: // CreateDC and CreateIC add an HDC handle to the HandleCollector; to remove it properly we need // to call DeleteHDC. #if TRACK_HDC Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("DC.DeleteHDC(hdc=0x{0:x8})", unchecked ((int)_hDC)))); #endif IntUnsafeNativeMethods.DeleteHDC(new HandleRef(this, _hDC)); _hDC = IntPtr.Zero; break; case DeviceContextType.Memory: // CreatCompatibleDC adds a GDI handle to HandleCollector, to remove it properly we need to call // DeleteDC. #if TRACK_HDC Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("DC.DeleteDC(hdc=0x{0:x8})", unchecked ((int)_hDC)))); #endif IntUnsafeNativeMethods.DeleteDC(new HandleRef(this, _hDC)); _hDC = IntPtr.Zero; break; // case DeviceContextType.Metafile: - not yet supported. case DeviceContextType.Unknown: default: return; // do nothing, the hdc is not owned by this object. // in this case it is ok if disposed throught finalization. } DbgUtil.AssertFinalization(this, disposing); }
public void Dispose(bool disposing) { if (_nativeHandle != IntPtr.Zero) { DbgUtil.AssertFinalization(this, disposing); if (_ownHandle) { IntUnsafeNativeMethods.DeleteObject(new HandleRef(this, _nativeHandle)); } _nativeHandle = IntPtr.Zero; if (disposing) { GC.SuppressFinalize(this); } } }
internal void Dispose(bool disposing) { if (_disposed) { return; } Disposing?.Invoke(this, EventArgs.Empty); _disposed = true; switch (_dcType) { case DeviceContextType.Display: Debug.Assert(disposing, "WARNING: Finalizing a Display DeviceContext.\r\nReleaseDC may fail when not called from the same thread GetDC was called from."); ((IDeviceContext)this).ReleaseHdc(); break; case DeviceContextType.Information: case DeviceContextType.NamedDevice: Interop.Gdi32.DeleteDC(new HandleRef(this, _hDC)); _hDC = IntPtr.Zero; break; case DeviceContextType.Memory: Interop.Gdi32.DeleteDC(new HandleRef(this, _hDC)); _hDC = IntPtr.Zero; break; // case DeviceContextType.Metafile: - not yet supported. case DeviceContextType.Unknown: default: return; // do nothing, the hdc is not owned by this object. // in this case it is ok if disposed throught finalization. } DbgUtil.AssertFinalization(this, disposing); }