Properly disposing of, and removing references to UserControls, to avoid memory leak

foreach (Control control in flowPanel.Controls) { if (control != NodeEditPanel.RootNodePanel) { control.Dispose(); } } flowPanel.Controls.Clear(); This is a pretty classic Winforms bug, many programmers have been bitten by it. Disposing a control also removes it from the parent’s Control collection. Most .NET collection classes trigger an InvalidOperationException when iterating them changes the collection but that … Read more