我有一个类,其中,基于某个事件,我initialize
一个新的基于表单的对话框和initialize
它。这个基于表单的对话框里面有其他控件。C#表单应用程序中的内存泄露
当dialog
已关闭时,我清除并处理掉在表单中创建的所有控件。不幸的是,似乎有些东西没有被处置,或者在删除之后仍留在记忆中。
形泵
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;
}
}
里面的formClass对象,在获得FormClosed事件,我处理了所有的控件和控件对象中的控制范围内,但仍有noticable内存泄漏。
Form类
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
}
}
有没有用我初始化了表单对象的方式可能的问题?那些没有被处置?
为什么你认为有内存泄漏?请注意,垃圾收集本身决定_when_实际释放内存。这种情况不一定是立即发生,但最终可以在gc发现没有足够的内存来完成下一个分配请求时完成。顺便说一句:如果你处理表单,它将自己处理所有的控件,不需要手动完成。 –
什么样的行为导致你相信存在内存泄漏? – tonythewest
尝试处置计时器以及表单对象? –