D
David Ei
I'm working on a C# Windows Forms application that is leaking memory.
I've been using the SciTech NetMem Profiler 2 (A really great tool,
BTW) to track down leaks. I've found one related to ToolTips that I've
been unable to fix.
We have a base form, from which we add and remove panels. When
created, the panels create tooltips which they associate with
themselves. When removed, the panels and their tooltips are Disposed.
The disposed tooltips are never garbage collected, though, because
there is a chain of delegates back to the base form referencing them.
Try as I might, I can't get the tooltip to unregister its delegates
when it is disposed. Either tooltips have a leak, or I'm not using or
disposing the tooltip correctly, I'm not sure which.
It would be very difficult to let the base form manage the tooltips,
because it doesn't know anything about the panels which are being
attached to it.
I've attached an extremely simplified test application which leaks
toolstips and illustrates the problem. To see the problem, run it
under the SciTech NetMem Profiler, and click the "Create/Dispose
MyPanel" button a few times (and see the tootips come and go). Then
press the "Collect Garbage" button and snapshot the heap. You'll see
that disposed MyPanels are gone, but disposed tooltips aren't.
Google won't let me add an attachment, so email me if you want a
zipped up solution file. Any help in resolving this problem would be
greatly appreciated.
Best regards,
David Ei
email: (e-mail address removed)
---------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ToolTipLeak
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ToolTip toolTip1;
private System.Windows.Forms.Button button1;
private ToolTipLeak.MyPanel panel1;
private System.Windows.Forms.Button button2;
private System.ComponentModel.IContainer components;
public Form1()
{
InitializeComponent();
InitializeMyPanel();
toolTip1.SetToolTip(this, "hi there");
}
private void InitializeMyPanel()
{
this.panel1 = new ToolTipLeak.MyPanel();
this.panel1.BackColor =
System.Drawing.SystemColors.ControlLightLight;
this.panel1.Location = new System.Drawing.Point(64, 48);
this.panel1.Name = "panel1";
this.Controls.Add(this.panel1);
this.panel1.TabIndex = 1;
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
components = null;
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.toolTip1 = new
System.Windows.Forms.ToolTip(this.components);
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// toolTip1
//
this.toolTip1.ShowAlways = true;
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 176);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(128, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Dispose Panel";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(64, 224);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(128, 23);
this.button2.TabIndex = 1;
this.button2.Text = "Collect Garbage";
this.button2.Click += new
System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
if (panel1 != null)
{
panel1.Dispose();
this.Controls.Remove(panel1);
panel1 = null;
this.button1.Text = "Create Panel";
}
else
{
InitializeMyPanel();
this.button1.Text = "Dispose Panel";
}
}
private void button2_Click(object sender, System.EventArgs e)
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
}
}
public class MyPanel : System.Windows.Forms.Panel
{
private System.ComponentModel.IContainer components;
private System.Windows.Forms.ToolTip toolTip2;
public MyPanel()
{
this.components = new System.ComponentModel.Container();
this.toolTip2 = new
System.Windows.Forms.ToolTip(this.components);
toolTip2.SetToolTip(this, "Am I owned by MyPanel?");
toolTip2.ShowAlways = true;
}
protected override void Dispose(bool disposing)
{
if (toolTip2 != null)
{
this.components.Remove(toolTip2);
toolTip2.RemoveAll();
toolTip2.Dispose();
toolTip2 = null;
}
if (components != null)
{
components.Dispose();
components = null;
}
base.Dispose (disposing);
}
}
}
I've been using the SciTech NetMem Profiler 2 (A really great tool,
BTW) to track down leaks. I've found one related to ToolTips that I've
been unable to fix.
We have a base form, from which we add and remove panels. When
created, the panels create tooltips which they associate with
themselves. When removed, the panels and their tooltips are Disposed.
The disposed tooltips are never garbage collected, though, because
there is a chain of delegates back to the base form referencing them.
Try as I might, I can't get the tooltip to unregister its delegates
when it is disposed. Either tooltips have a leak, or I'm not using or
disposing the tooltip correctly, I'm not sure which.
It would be very difficult to let the base form manage the tooltips,
because it doesn't know anything about the panels which are being
attached to it.
I've attached an extremely simplified test application which leaks
toolstips and illustrates the problem. To see the problem, run it
under the SciTech NetMem Profiler, and click the "Create/Dispose
MyPanel" button a few times (and see the tootips come and go). Then
press the "Collect Garbage" button and snapshot the heap. You'll see
that disposed MyPanels are gone, but disposed tooltips aren't.
Google won't let me add an attachment, so email me if you want a
zipped up solution file. Any help in resolving this problem would be
greatly appreciated.
Best regards,
David Ei
email: (e-mail address removed)
---------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ToolTipLeak
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ToolTip toolTip1;
private System.Windows.Forms.Button button1;
private ToolTipLeak.MyPanel panel1;
private System.Windows.Forms.Button button2;
private System.ComponentModel.IContainer components;
public Form1()
{
InitializeComponent();
InitializeMyPanel();
toolTip1.SetToolTip(this, "hi there");
}
private void InitializeMyPanel()
{
this.panel1 = new ToolTipLeak.MyPanel();
this.panel1.BackColor =
System.Drawing.SystemColors.ControlLightLight;
this.panel1.Location = new System.Drawing.Point(64, 48);
this.panel1.Name = "panel1";
this.Controls.Add(this.panel1);
this.panel1.TabIndex = 1;
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
components = null;
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.toolTip1 = new
System.Windows.Forms.ToolTip(this.components);
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// toolTip1
//
this.toolTip1.ShowAlways = true;
//
// button1
//
this.button1.Location = new System.Drawing.Point(64, 176);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(128, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Dispose Panel";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(64, 224);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(128, 23);
this.button2.TabIndex = 1;
this.button2.Text = "Collect Garbage";
this.button2.Click += new
System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
if (panel1 != null)
{
panel1.Dispose();
this.Controls.Remove(panel1);
panel1 = null;
this.button1.Text = "Create Panel";
}
else
{
InitializeMyPanel();
this.button1.Text = "Dispose Panel";
}
}
private void button2_Click(object sender, System.EventArgs e)
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
}
}
public class MyPanel : System.Windows.Forms.Panel
{
private System.ComponentModel.IContainer components;
private System.Windows.Forms.ToolTip toolTip2;
public MyPanel()
{
this.components = new System.ComponentModel.Container();
this.toolTip2 = new
System.Windows.Forms.ToolTip(this.components);
toolTip2.SetToolTip(this, "Am I owned by MyPanel?");
toolTip2.ShowAlways = true;
}
protected override void Dispose(bool disposing)
{
if (toolTip2 != null)
{
this.components.Remove(toolTip2);
toolTip2.RemoveAll();
toolTip2.Dispose();
toolTip2 = null;
}
if (components != null)
{
components.Dispose();
components = null;
}
base.Dispose (disposing);
}
}
}