I have a Windows Form screen with a menu bar docked at the left-hand side on the form, similar to the one of Outlook 2003, and a panel taking the rest of the right-hand side of the form.
My left menu contains items. Upon clicking on those items, I want to load the appropriate user control (acting as "sub-forms") on the right-hand side panel. I have managed to do that and it is working well.
The worry I have though is whether or not my usercontrols are being disposed properly or if they remain on memory when the user clicks another menu item (and as such, loads another usercontrol/subform).
The code is similar to this:
private void ultraExplorerBar1_ItemClick(object sender, Infragistics.Win.UltraWinExplorerBar.ItemEventArgs e)
{
panel1.Controls.Clear();
if (e.Item.Key == "Action")
{
ActionUserControl subForm = new ActionUserControl();
panel1.Controls.Add(subForm);
subForm.Show();
}
else if (e.Item.Key == "Options")
{
OptionsUserControl subForm = new OptionsUserControl();
panel1.Controls.Add(subForm);
subForm.Show();
}
}
I am not sure if the panel1.Controls.Clear() I'm calling at the beginning of the method is enough for the usercontrols not being used anymore to be garbage-collected in due-time?
To my understanding, there shouldn't be any reference left to it in the application once cleared from the panel but I am not sure if I should be doing something else or of a reasonably lightweight tool I could use to confirm that my subforms are disposed as they should and that it doesn't cause some kind of memory/resources leak?
Use this loop before calling panel1.Controls.Clear() and be sure that the Dispose() method in the user controls are in turn disposing of any disposable resources.
foreach (Control control in panel1.Controls)
{
control.Dispose();
}
A for each loop will not do the job. Since after each dispose the elements and indexes are changed.
You need a while loop disposing the top most control each time.
In the following code that I use, I have a panel with user controls in it. I go into the first control and dispose of its controls then dispose of the control itself.
After disposing of all the controls trigger a clear to reset the index.
While pnlMain.Controls.Count > 0
While pnlMain.Controls(0).Controls.Count > 0
pnlMain.Controls(0).Controls(0).Dispose()
End While
pnlMain.Controls(0).Dispose()
End While
pnlMain.Controls.Clear()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With