473,320 Members | 1,862 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,320 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 2659
"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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.