我找不到为什么在SharpDX中不释放内存的原因

时间:2018-07-12 02:15:00

标签: sharpdx

我在某些Winform应用程序上进行制作,我注意到程序的内存问题。

这是我的winform自定义控制代码。

using System;
using System.Drawing;
using System.Windows.Forms;

using DX = SharpDX;
using D2D = SharpDX.Direct2D1;
using SharpDX.Mathematics.Interop;
using DW = SharpDX.DirectWrite;

namespace WinFormTest
{
    public partial class BitmapSurface : Control
    {
        D2D.Factory d2dFactory;
        DW.Factory dwFactory;
        D2D.WindowRenderTarget wrt;
        D2D.BitmapRenderTarget brt;

        Rectangle clippingRect = new Rectangle(0, 0, 100, 100);

        public BitmapSurface()
        {
            InitializeComponent();
            InitializeRenderer();
        }

        DX.Size2 clientSize2;
        DX.Size2F clientSize2f;

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            if (clientSize2 == null)
                clientSize2 = new DX.Size2();
            if (clientSize2f == null)
                clientSize2f = new DX.Size2F();

            clientSize2.Width = ClientSize.Width;
            clientSize2.Height = ClientSize.Height;

            clientSize2f.Width = ClientSize.Width;
            clientSize2f.Height = ClientSize.Height;


            if (wrt != null)
            {
                wrt.Resize(clientSize2);
            }
            if (brt != null)
            {
                brt.Dispose();
                brt = new D2D.BitmapRenderTarget(wrt, D2D.CompatibleRenderTargetOptions.None, clientSize2f, null, null);
                brt.AntialiasMode = D2D.AntialiasMode.Aliased;
            }

            this.Invalidate();
        }

        private D2D.SolidColorBrush GetBrush(float r, float g, float b, float a = 255)
        {
            var brush = new D2D.SolidColorBrush(
                wrt, new RawColor4(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f));
            return brush;
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);

            clippingRect = this.ClientRectangle;
            var rect = clippingRect;

            DrawBitmap();
        }

        public void InitializeRenderer()
        {
            int width = this.ClientSize.Width;
            int height = this.ClientSize.Height;

            D2D.HwndRenderTargetProperties hrtp = new D2D.HwndRenderTargetProperties();
            hrtp.Hwnd = this.Handle;
            hrtp.PixelSize = new DX.Size2(width, height);

            // D2DFactory
            if (d2dFactory == null)
                d2dFactory = new D2D.Factory();

            // DirectWrite
            if (dwFactory == null)
                dwFactory = new DW.Factory();

            wrt = new D2D.WindowRenderTarget(d2dFactory, new D2D.RenderTargetProperties(), hrtp);
            wrt.AntialiasMode = D2D.AntialiasMode.Aliased;

            brt = new D2D.BitmapRenderTarget(wrt, D2D.CompatibleRenderTargetOptions.None, new DX.Size2F(width, height), null, null);
            brt.AntialiasMode = D2D.AntialiasMode.Aliased;
        }

        public void DrawBitmap()
        {
            brt.BeginDraw();
            brt.Clear(new RawColor4(0, 0, 0, 1));

            // Draw Something
            for (int i = 0; i < 1000; i++)
            {
                brt.DrawLine(
                    new RawVector2(0, i),
                    new RawVector2(90, 90 + i),
                    GetBrush(255, 255, 255));
            }

            for (int i = 0; i < 1000; i++)
            {
                brt.FillRectangle(
                    new RawRectangleF(90, 0 + i, 150, 10 + i),
                    GetBrush(255 - i, 255 - i, 255 - i));
            }

            brt.EndDraw();

            wrt.BeginDraw();
            wrt.DrawBitmap(brt.Bitmap, 1, D2D.BitmapInterpolationMode.NearestNeighbor);
            wrt.EndDraw();
        }
    }
}

我的程序的主窗体可以有许多孩子,孩子可以用SharpDX绘制东西。

内存使用量增加当我打开子窗体但关闭子窗体和GC之后。收集的内存使用量不会减少。

SharpDX用法不好吗?

0 个答案:

没有答案