Shared Event code

Use this forum to read, ask or inform about memory issues in the .NET Framework and third party components.

Moderator: SciTech Software

Shared Event code

Postby stijn » Wed Oct 17, 2007 1:47 pm

The situation : My program has a main MDI Form, in which different other forms can be opened, when the application is closed, it remembers which form was closed last, and it will open this form the next time you start the application .

One of these forms has 2 toolstrips, the second toolstrip : the "search" toolstript isnt reparented to the MDI Parent, and has 2 combo's, a textbox and a button in it .
Both combo's and the textbox trigger a click on the button when the user presses "ENTER" in those controls

Considering one of my previous problems, the dispose of this form already contains :
if (this.toolStrip1 != null)
this.toolStrip1.Visible = false;
this.toolStrip1 = null;
if (this.toolStrip2 != null)
this.toolStrip2.Visible = false;
this.toolStrip2 = null;

When is start the application, and close this form that was automatically opened , then it stays in memory, as disposed , with following rootpath :
System.Windows.Forms KeyEventHandler
System.ComponentModel EventHandlerList.ListEntry
System.ComponentModel EventHandlerList
System.Windows.Forms ToolStripTextBox
System.Windows.Forms ToolStripTextBox.ToolStripTextBoxControl
Microsoft.Win32 UserPreferenceChangedEventHandler
Microsoft.Win32 SystemEvents.SystemEventInvokeInfo
Microsoft.Win32 SystemEvents.SystemEventInvokeInfo[]
System.Collections.Generic List<SystemEvents.SystemEventInvokeInfo>
System.Collections.Generic Dictionary<object, List<SystemEvents.SystemEventInvokeInfo>>.Entry[]
System.Collections.Generic Dictionary<object, List`1>
Microsoft.Win32 SystemEvents _handlers

Now, In my code , i had following thing happening :

this.cbVT.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtSearch_KeyDown);
this.cbComboSearch.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtSearch_KeyDown);
this.txtSearch.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtSearch_KeyDown);

(generated by VS2005)

this can be fixed in by manually unhooking the keydown event on the
by adding following to your dispose
if (this.txtSearch != null)
this.txtSearch.KeyDown -= new System.Windows.Forms.KeyEventHandler(this.txtSearch_KeyDown);

It does not appear to be necessary to do the same for both combos :
if (this.cbVT != null)
this.cbVT.KeyDown -= new System.Windows.Forms.KeyEventHandler(this.txtSearch_KeyDown);
if (this.cbComboSearch != null)
this.cbComboSearch.KeyDown -= new System.Windows.Forms.KeyEventHandler(this.txtSearch_KeyDown);

The only thing i still dont understand, is why this "leak" only happens when the window was opened by the program, and not when a user opens it afterwards !
Posts: 23
Joined: Thu Jun 28, 2007 2:57 pm

Postby Andreas Suurkuusk » Thu Oct 18, 2007 6:07 pm

By removing the event handler you avoid that the text box keeps a reference to the form and prevents it from being GCed. But I assume that the search text box should also be GCed, which it will not be since it's still referenced by the UserPreferenceChangedEventHandler.

This problem seems related to the topic of my blog post Memory Leak in ToolStripTextBoxControl.

This post might give you some ideas on how to solve this problem.
Best regards,

Andreas Suurkuusk
SciTech Software AB
Andreas Suurkuusk
Posts: 957
Joined: Wed Mar 02, 2005 7:53 pm
Location: Sweden

Return to Known Memory Issues

Who is online

Users browsing this forum: No registered users and 1 guest

SciTech Software logo

© Copyright 2001-2016. SciTech Software AB
All rights reserved.

SciTech Software AB
Kartvägen 21
SE-175 46 Järfälla


Telephone: +46-706868081