473,473 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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);
}
}
}
Nov 16 '05 #1
1 2876
"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);
}
}
}

Nov 16 '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...
0
by: Bonna | last post by:
Hi, I've set up a helpsystem for my application using html help 1.x and the dotnet HelpProvider object. It's annoying that that you have to fill in the tooltips yourself (for providing help on...
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...
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...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.