I’ve inherited some code and wanted to run this modification by you all, my concern is memory management.
Let us say I have a “base” Form with a bunch of buttons that open “dialog” forms. What is the recommended pattern for opening the dialog forms? Currently we display the “dialog” form like so (in the “base” Form code, upon button click):
ChangePasswordForm frm = new ChangePasswordForm();
frm.ShowDialog();
Then close it like so (in the “dialog” form code):
private void bCancel_Click(object sender, EventArgs e)
{
this.Close();
//this.Dispose(); <-- this is what I am considering adding.
}
My rationale for adding Dispose is that I am worried if this form is displayed and closed many times that each time a new instance of the form is created and its resources are never really released — is this correct? Also, if the form has the “close” X in the top right, should I put a Dispose() call in the FormClosed event as well?
Thanks in advance.
I would use a
usingstatement:Combine this with a DialogResult:
Setting the DialogResult, will close the Dialog, and the caller/owner has some feedback.
And you don’t have to worry about Close or Dispose.