Exemple #1
0
        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);

                        Interop.Gdi32.DeleteObject(Hfont);
                        Hfont         = IntPtr.Zero;
                        _ownHandle    = false;
                        deletedHandle = true;
                    }
                }
            }

            if (disposing && (deletedHandle || !_ownHandle))
            {
                GC.SuppressFinalize(this);
            }
        }
        internal void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }

            Disposing?.Invoke(this, EventArgs.Empty);

            disposed = true;

            DisposeFont(disposing);

            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)this.hDC))));
#endif

                UnsafeNativeMethods.DeleteDC(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)this.hDC))));
#endif
                UnsafeNativeMethods.DeleteDC(new HandleRef(this, hDC));

                hDC = IntPtr.Zero;
                break;

            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);
        }
Exemple #3
0
        protected virtual void Dispose(bool disposing)
        {
            if (dc != null && nativeHandle != IntPtr.Zero)
            {
                DbgUtil.AssertFinalization(this, disposing);

                dc.DeleteObject(nativeHandle, GdiObjectType.Brush);

                nativeHandle = IntPtr.Zero;
            }

            if (disposing)
            {
                GC.SuppressFinalize(this);
            }
        }
Exemple #4
0
        public void Dispose(bool disposing)
        {
            if (nativeHandle != IntPtr.Zero)
            {
                DbgUtil.AssertFinalization(this, disposing);

                if (ownHandle)
                {
                    Interop.Gdi32.DeleteObject(nativeHandle);
                }

                nativeHandle = IntPtr.Zero;

                if (disposing)
                {
                    GC.SuppressFinalize(this);
                }
            }
        }
Exemple #5
0
        void Dispose(bool disposing)
        {
            if (_nativeHandle != IntPtr.Zero && _dc != null)
            {
                DbgUtil.AssertFinalization(this, disposing);

                _dc.DeleteObject(_nativeHandle, GdiObjectType.Pen);
                _nativeHandle = IntPtr.Zero;
            }

            if (_wndBrush != null)
            {
                _wndBrush.Dispose();
                _wndBrush = null;
            }

            if (disposing)
            {
                GC.SuppressFinalize(this);
            }
        }
Exemple #6
0
        internal void Dispose(bool disposing)
        {
            if (DeviceContext != null)
            {
                DbgUtil.AssertFinalization(this, disposing);

                try
                {
                    // Restore original dc.
                    DeviceContext.RestoreHdc();

                    if (_disposeDc)
                    {
                        DeviceContext.Dispose(disposing);
                    }

                    if (_graphics != null)    // if created from a Graphics object...
                    {
                        _graphics.ReleaseHdcInternal(DeviceContext.Hdc);
                        _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
                {
                    DeviceContext = null;
                }
            }
        }