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); } }
/// <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); } } }
public void DeleteObject(IntPtr handle, GdiObjectType type) { IntPtr handleToDelete = IntPtr.Zero; switch (type) { case GdiObjectType.Pen: if (handle == hCurrentPen) { IntPtr currentPen = IntUnsafeNativeMethods.SelectObject(new HandleRef(this, this.Hdc), new HandleRef(this, hInitialPen)); Debug.Assert(currentPen == hCurrentPen, "DeviceContext thinks a different pen is selected than the HDC"); hCurrentPen = IntPtr.Zero; } handleToDelete = handle; break; case GdiObjectType.Brush: if (handle == hCurrentBrush) { IntPtr currentBrush = IntUnsafeNativeMethods.SelectObject(new HandleRef(this, this.Hdc), new HandleRef(this, hInitialBrush)); Debug.Assert(currentBrush == hCurrentBrush, "DeviceContext thinks a different brush is selected than the HDC"); hCurrentBrush = IntPtr.Zero; } handleToDelete = handle; break; case GdiObjectType.Bitmap: if (handle == hCurrentBmp) { IntPtr currentBmp = IntUnsafeNativeMethods.SelectObject(new HandleRef(this, this.Hdc), new HandleRef(this, hInitialBmp)); Debug.Assert(currentBmp == hCurrentBmp, "DeviceContext thinks a different brush is selected than the HDC"); hCurrentBmp = IntPtr.Zero; } handleToDelete = handle; break; } IntUnsafeNativeMethods.DeleteObject(new HandleRef(this, handleToDelete)); }