Sunday 24 January 2010

GDI object leaking example

Finally I have a bit of time to post what caused us GDI objects leaking troubles. I hope it can same time to others who are dealing with the same problem. We ensured that all Graphics, Brushes etc. are destroyed correctly but the application was still leaking. I would like to show part of code which we found when investigating the GDI object leaking issue. We used component which should show first and last and in tooltip some more info. Tooltip was created every time the person was set but the old one was not cleaned up. Each Tooltip use couple of GDI objects which were not released until the application was closed.
public Person SelectedPerson
{
  get { return currentPerson; }
  set
  {
    currentPerson = value;
    Text = GetDescription(currentPerson);
    if (currentPerson != null)
    {
      ToolTip toolTip = new ToolTip();
      toolTip.SetToolTip(this, GetDetailDescription(currentPerson));
    }
  }
}