我有一個類,其中,基於某個事件,我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
嘗試處置計時器以及表單對象? –