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.
Coline
source
share