Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automate clean up of ToolTip in UserControl

I've been tracking down a memory leak and I've narrowed it down to a ToolTip that is allocated in a UserControl-derived class.

The ToolTip is allocated in the allocated in the control's constructor and initialized in the Load event like this:

public class CommonProfile : System.Windows.Forms.UserControl
{
    private ToolTip toolTip1;

    ...

    public CommonProfile()
    {
        InitializeComponent();

        // Create the ToolTip and associate with the Form container.
        toolTip1 = new ToolTip(this.components);
    }

    private void CommonProfile_Load(object sender, System.EventArgs e)
    {
        // Set up the delays for the ToolTip.
        toolTip1.AutoPopDelay = 5000;
        toolTip1.InitialDelay = 1000;
        toolTip1.ReshowDelay = 500;
        // Force the ToolTip text to be displayed whether or not the form is active.
        toolTip1.ShowAlways = true;

        // Set up the ToolTip text
        toolTip1.SetToolTip(this.btnDeleteEntry, "Delete this Profile");
        toolTip1.SetToolTip(this.lblProfileType, "Edit this Profile");
        toolTip1.SetToolTip(this.lblProfileData, "Edit this Profile");
        toolTip1.SetToolTip(this.picFlagForUpdate, "Toggle Flag for Update");
    }    
}

The control's parent has a lifetime that exceeds the control's lifetime. This control is created on the fly and added to a panel control and then subsequently removed from the panel control.

I found that the control's Dispose member was not getting called, apparently because references to the ToolTip remain.

I added a Shutdown method like this:

public void Shutdown()
{
    toolTip1.RemoveAll();
}

Calling the Shutdown method eliminates the leak and Dispose is eventually called.

Unfortunately, this solution requires that whoever uses the control remembers to call the Shutdown method when they are finished with it.

I'd like to know if there is some way that I can automate this so that it happens without the need to call the Shutdown method explicitly.

like image 249
Avalanchis Avatar asked Nov 22 '11 14:11

Avalanchis


People also ask

How many ToolTip objects are required on a Windows Form?

Add a ToolTip object to your form. One object is enough for the entire form.

How do I use ToolTip in WinForms?

From within the designer: 1) From the ToolBox, drop a ToolTip control to your form. 2) Check the properties of this toolTip1 object to make sure Active is set to true. 3) Click on your button, and in its Property page, set the ToolTip on toolTip1 entry.


2 Answers

You aren't showing the code for how you are disposing your UserControl from your Panel.

Just calling:

panel1.Controls.Remove(userControl1);

isn't going to dispose the UserControl.

You need to specifically call:

userControl1.Dispose();

which will also automatically remove it from the Panel. In your UserControl, if you need to do your own clean up, try subscribing to it's own Dispose event:

private ToolTip toolTip1;

public UserControl1() {
  InitializeComponent();
  // tooltip initialization
  this.Disposed += UserControl1_Disposed;
}

private void UserControl1_Disposed(object sender, EventArgs e) {
  if (toolTip1 != null)
    toolTip1.Dispose();
}
like image 147
LarsTech Avatar answered Sep 20 '22 14:09

LarsTech


Also in your control dispose method you need to explicitly call dispose on your tooltip.

like image 44
PRR Avatar answered Sep 20 '22 14:09

PRR