I have a class, where, based on a certain event, I initialize
a new form based dialog and initialize
it. This form based dialog has other controls inside it.
When the dialog
is closed, I clear and dispose off all the controls which are created in the form. Unfortunately, it seems that something is not getting disposed, or remaining in memory even after the removal.
Form pump
class someClass
{
System.Timers.Timer someTimer;
public void CallToChildThread(Object stateInfo)
{
// check some event
// if true, fire event
}
someClass()
{
someTimer= new System.Threading.Timer(CallToChildThread,
autoEvent, 1000, 250);
_show += new EventHandler(eventCheck);
}
void eventCheck()
{
formClass formClassObject = new formClass(); //create form
formClassObject.someFunction(); // has some other function and does a showDialog on self
formClassObject.Dispose();
formClassObject = null;
}
}
Inside the formClass object, upon getting the FormClosed event, I dispose off all the controls and controls within the controls within the object, but there is still a noticable memory leak.
Form class
public partial class formClass
{
//Initialize a bunch of managed resources to null
someOtherForm form2;
someOtherForm form3;
//connect some events on child forms to buttons on this form object
this.form2.cancelButtonClicked += someFunction;
this.form3.cancelButtonClicked += someFunction;
// Form closed Event
private void formClass_FormClosed(object sender, FormClosedEventArgs e)
{
//set form2 and form3 visibility to false
// clear AND dispose all controls of form2
// clear AND dispose all controls of form3
//set form2 and form3 to null
// clear AND dispose off all controls of formClass
// Dispose this (formClass) object
}
}
Is there a possible issue with the way I have initialized the form objects? Are those not getting disposed?