I was debugging resource leaks in my application and created a test app to test GDI object leaks. In OnPaint I create new icons and new bitmaps without disposing them. After that I check the increase of GDi objects in task manager for each of the cases. However, if I keep repainting the main window of my app, the number of GDI objects increases for icons, but there is no change for bitmaps. Is there any particular reason why icons are not getting cleaned up same way as bitmaps?
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// 1. icon increases number of GDI objects used by this app during repaint.
//var icon = Resources.TestIcon;
//e.Graphics.DrawIcon(icon, 0, 0);
// 2. bitmap doesn't seem to have any impact (only 1 GDI object)
//var image = Resources.TestImage;
//e.Graphics.DrawImage(image, 0, 0);
}
}
Test Result:
- No icons and bitmaps - 30 GDI objects
- With bitmaps - 31 GDI object, the number doesn't change.
- With icons - 31 and then the number increases if you repaint the window.