How to create a GDI leak in Windows Forms!

I am investigating the leak of GDI resources in a large application. To further understand how these problems arise, I created a very small application, which I intentionally made "leaky." Here is a simple user control that should lead to the creation of 100 Pen objects:

public partial class TestControl: UserControl
{
    private List pens = new List ();

    public TestControl ()
    {
        InitializeComponent ();

        for (int i = 0; i <100; i ++)
        {
            pens.Add (new Pen (new SolidBrush (Color.FromArgb (255, i * 2, i * 2, 255 - i * 2))));
        }

        this.Paint + = new PaintEventHandler (TestControl_Paint);
    }

    void TestControl_Paint (object sender, PaintEventArgs e)
    {
        for (int i = 0; i <100; i ++)
        {
            e.Graphics.DrawLine (pens [i], 0, i, Width, i);
        }
    }
}

However, when I create an instance of my object and add it to the form, looking at my application with the TaskManager, I currently see ~ 37 GDI objects. If I re-add new TestObject controls to my form, I still see only ~ 37 GDI objects.

What's going on here! I thought that the constructor for System.Drawing.Pen would use the GDI + API to create a new Pen, thus using the new GDI object.

I must be losing my mind. If I cannot write a simple test application that creates GDI objects, how can I create one that runs them?

Any help would be greatly appreciated.

Best regards, Colin E.

+3
source share
6

GDI + GDI? , - , .NET System.Drawing, GDI.

, , , AQTime.

, GDI? ? , GDI + GDI? GDI , ?

+2

. Load:

for (int i = 0; i < 100; i++)
    {
        pens.Add(new Pen(new SolidBrush(Color.FromArgb(255, i * 2, i * 2, 255 - i * 2))));
    }

Paint :

void TestControl_Paint(object sender, PaintEventArgs e)
{
    for (int i = 0; i < 100; i++)
    {
        e.Graphics.DrawLine(new Pen(new SolidBrush(Color.FromArgb(255, i * 2, i * 2, 255 - i * 2))), 0, i, Width, i);
    }
}

. / GDI...

, .

+2

GDI .NET, GDI :

[DllImport("gdi32.dll", EntryPoint="CreatePen", CharSet=CharSet.Auto, SetLastError=true, ExactSpelling=true)]
private static extern IntPtr CreatePen(int fnStyle, int nWidth, int crColor);

CreatePen(0, 0, 0); //(PS_SOLID, 0=1px wide, 0=black)

Blingo blango, GDI.

, GDI. , GDI WinForm - .

+1

, , , :

GDI

GDI, , . ( GDI + FAQ)

, CLR GDI, TaskManager. , GDI + GDI.

GDI. GDI.

void Form1_Paint(object sender, PaintEventArgs e) 
{
    Random r = new Random();
    while (true)
    {
        for (int i = 0; i < 100; i++)
        {
            e.Graphics.DrawLine(
            new Pen(new SolidBrush(Color.FromArgb(r.Next()))), 0, i, Width, i);
        }
    }
}
0

, .

delphi ,
WinAPI CreateFont(), GDI.

0

. . Dispose.

    Form _test = null;
    for (int i = 0; i < 20; i++)
    {
        _test = new Form();
        _test.Visible = false;
        _test.Show();
        _test.Hide();
        _test.Dispose();
    }

Dispose . , Dispose , User GDI .

, , , , .

0
source

Source: https://habr.com/ru/post/1708565/


All Articles