internal void Dispose(bool disposing) { bool deletedHandle = false; if (ownHandle) { if (!ownedByCacheManager || !disposing) { // If we were ever owned by the CacheManger and we're being disposed // we can be sure that we're not in use by any DC's (otherwise Dispose() wouldn't have been called) // skip the check IsFontInUse check in this case. // Also skip the check if disposing == false, because the cache is thread-static // and that means we're being called from the finalizer. if (everOwnedByCacheManager || !disposing || !DeviceContexts.IsFontInUse(this)) { Debug.Assert(hFont != IntPtr.Zero, "Unexpected null hFont."); DbgUtil.AssertFinalization(this, disposing); IntUnsafeNativeMethods.DeleteObject(new HandleRef(this, hFont)); #if TRACK_HFONT Debug.WriteLine(DbgUtil.StackTraceToStr(String.Format("DeleteObject(HFONT[0x{0:x8}]))", (int)this.hFont))); #endif hFont = IntPtr.Zero; ownHandle = false; deletedHandle = true; } } } if (disposing && (deletedHandle || !ownHandle)) { GC.SuppressFinalize(this); } }
internal void Dispose(bool disposing) { if (this.dc != null) { DbgUtil.AssertFinalization(this, disposing); try { // Restore original dc. this.dc.RestoreHdc(); if (this.disposeDc) { this.dc.Dispose(disposing); } if (this.graphics != null) // if created from a Graphics object... { this.graphics.ReleaseHdcInternal(this.dc.Hdc); this.graphics = null; } } catch (Exception ex) { if (ClientUtils.IsSecurityOrCriticalException(ex)) { throw; // rethrow the original exception. } Debug.Fail("Exception thrown during disposing: \r\n" + ex.ToString()); } finally { this.dc = null; } } }
protected virtual void Dispose(bool disposing) { if (dc != null && this.nativeHandle != IntPtr.Zero) { DbgUtil.AssertFinalization(this, disposing); dc.DeleteObject(this.nativeHandle, GdiObjectType.Brush); this.nativeHandle = IntPtr.Zero; } if (disposing) { GC.SuppressFinalize(this); } }
/// <devdoc> /// </devdoc> public void Dispose(bool disposing) { if (this.nativeHandle != IntPtr.Zero) { DbgUtil.AssertFinalization(this, disposing); if (this.ownHandle) { IntUnsafeNativeMethods.DeleteObject(new HandleRef(this, this.nativeHandle)); } this.nativeHandle = IntPtr.Zero; if (disposing) { GC.SuppressFinalize(this); } } }
void Dispose(bool disposing) { if (this.nativeHandle != IntPtr.Zero && dc != null) { DbgUtil.AssertFinalization(this, disposing); dc.DeleteObject(this.nativeHandle, GdiObjectType.Pen); this.nativeHandle = IntPtr.Zero; } if (this.wndBrush != null) { this.wndBrush.Dispose(); this.wndBrush = null; } if (disposing) { GC.SuppressFinalize(this); } }
/// <include file='doc\IDeviceContext.uex' path='docs/doc[@for="DeviceContext.Dispose1"]/*' /> internal void Dispose(bool disposing) { if (disposed) { return; } if (this.Disposing != null) { this.Disposing(this, EventArgs.Empty); } this.disposed = true; #if !DRAWING_NAMESPACE DisposeFont(disposing); #endif switch (this.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)this.hDC)))); #endif IntUnsafeNativeMethods.DeleteHDC(new HandleRef(this, this.hDC)); this.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)this.hDC)))); #endif IntUnsafeNativeMethods.DeleteDC(new HandleRef(this, this.hDC)); this.hDC = IntPtr.Zero; break; // case DeviceContextType.Metafile: - not yet supported. #if WINFORMS_PUBLIC_GRAPHICS_LIBRARY case DeviceContextType.Metafile: IntUnsafeNativeMethods.CloseEnhMetaFile(new HandleRef(this, this.Hdc)); this.hDC = IntPtr.Zero; break; #endif 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); }