Always call DataBindings.Clear when disposing a form
Whenever you use control.DataBindings.Add to do data binding you need to take special care to clean up the databindings. The reason is that internally there is a reference to the bound control that is not released unless you do it in the Dispose. Therefore, in each base form in your control hierchy, put in some code to clean them up.
/// <summary> /// Clean up any resources being used./// </summary>protected override void Dispose( bool disposing ){
// This call is recommended by an MSDN articleGlobal
.ClearBindings(this);if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );}
...
/// <summary>/// According to article on MSDN, this will make bound object available for gc/// </summary>/// <param name="c"></param>public static void ClearBindings(Control c){
Binding[] bindings
= new Binding[c.DataBindings.Count]; c.DataBindings.CopyTo(bindings, 0);
c.DataBindings.Clear();
foreach (Binding binding in bindings)
{
TypeDescriptor.Refresh(binding.DataSource);
}
foreach (Control cc in c.Controls){
ClearBindings(cc);
}
}
This recommendation comes from an MSDN article along with many more excellent databinding recommendations.