473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Leaking ToolTips

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: de******@eieioh.net

---------------------------------------------------------------------

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);
}
}
}
Jul 21 '05 #1
1 2662
"David Ei" <de******@eieioh.net> wrote in message
news:9d**************************@posting.google.c om...
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.
Yep, we use it, it's very good.

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.
Yes. If you decompile the ToolTip class, it looks like whoever wrote it
copied the event connection code to where they wanted to disconnect, but
forgot to change the += to -=.

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.
I wrote a very simple tooltip class which has the minimum functionality that
I need (GetToolTip, SetToolTip), that replaces the SWF one. It involves
P/Invoking to SendMessage, using the TTM_ADDTOOL etc messages.

I can probably post the code if that will help, but bear in mind (a) I only
wrote it yesterday so it hasn't been properly tested (b) I don't know how it
will interact with the designer and (c) my knowledge of Win32 is pretty poor
so my solution may well have problems of its own.

Stu


Best regards,

David Ei
email: de******@eieioh.net

---------------------------------------------------------------------

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);
}
}
}

Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: shraddha | last post by:
Hi I want to implement dynamic interactive tooltips in java so that the user can expand / collapse them. Also, if there are hyperlinks in the tooltip text, the user should be able to follow up...
3
by: Wayne | last post by:
Is anybody aware of other software conflicting with Access and disabling the tool tips. I've had this problem in the past and now have it again after rebuilding my desktop and notebook. Both fail...
1
by: Wayne Aprato | last post by:
Tooltips in Access have not worked on my machine for a long time despite several reinstalls of the operating system and applications at fairly regular intervals. I have finally pinned down the...
1
by: David Ei | last post by:
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...
4
by: Cesario | last post by:
I'm trying to display formatted multi-line text in tooltips. It seems that the newline character works fine whereas the 'tab' character is causing problems and can't display properly. More...
4
by: Jeff Beem | last post by:
I have a class that inherits from TreeView. The treeview has tooltips for the nodes but they're showing up behind the form. Has anyone seen this? Are there any known fixes? Thanks in advance,...
21
by: PlainDave | last post by:
Hi, Is there a way to make windows tooltips stay up longer than the default 5 seconds using CSS in a web page? I'd prefer to have it stay visible as long as the mouse is over the "whatever." The...
1
by: Robert Smith | last post by:
Hello, I have a Tree that contains tooltips, however when I right click on a node of the tree the context menu covers up the tooltips and it looks rather unprofessional. I wish to hide the...
1
by: TyBreaker | last post by:
I have a simple form with a toolstrip on it and some toolstrip buttons. Tooltips work fine. But then I use the ShAppBarMessage API call to register the form as an Application Desktop Toolbar. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.