Forms not completely disposing...

G

Guest

Compact Framework 2 - SP2

Setup the project...

Form1 that contains two buttons and an input panel.
Form2 that contains one label and an input panel.

Form2's constructor has an integer as an parameter. This will be the
identifier when the form shows it status message. Everytime the input panel
changes enable state the form will popup a messagebox with the id and the
panel's state.

Form1's buttons are exactly the same in that both will create a variable for
Form2 then call the ShowDialog method. The difference is that one will call
the dispose method after displaying the form.


Setup the problem...

When you click either button the Form2 will popup then you can click the OK
button. Once you click the button to close the form the messagebox will popup
and state "Form2 - 0: False". Close the messagebox. At this point I expect
the Form2 variable to be dispose and irrelevant.


Enter the problem...

Now if you click Form1's input panel you will get a message from the Form2
that states "Form2 - 0: True". At this point you shouldn't be running any
code in Form2...

Code below...

public partial class Form1 : Form
{
protected int count;

public Form1()
{
InitializeComponent();

count = 0;
}

private void button1_Click(object sender, EventArgs e)
{
Form2 dlg = new Form2(count++);

try
{
dlg.ShowDialog();
}
finally
{
dlg.Dispose();
}
}

private void button2_Click(object sender, EventArgs e)
{
Form2 dlg = new Form2(count++);
dlg.ShowDialog();
}

}


public partial class Form2 : Form
{
protected int _id;

public Form2(int ID)
{
InitializeComponent();

_id = ID;
label1.Text = "Form2: " + _id.ToString();

inputPanelForm2.Enabled = true;
}

private void inputPanelForm2_EnabledChanged(object sender, EventArgs
e)
{
MessageBox.Show("Form2 - " + _id.ToString() + ": " +
inputPanelForm2.Enabled.ToString());
}

private void Form2_Closing(object sender, CancelEventArgs e)
{
inputPanelForm2.Enabled = false;
}
}
 
C

chris-s

IIRC this is quite an old issue, basically, you need to remove any
events that you assign to an input panel control when closing/
disposing the form.

Chris
 
H

HedgehogJim

Compact Framework 2 - SP2

Setup the project...

Form1 that contains two buttons and an input panel.
Form2 that contains one label and an input panel.

Form2's constructor has an integer as an parameter. This will be the
identifier when the form shows it status message. Everytime the input panel
changes enable state the form will popup a messagebox with the id and the
panel's state.

Form1's buttons are exactly the same in that both will create a variable for
Form2 then call the ShowDialog method. The difference is that one will call
the dispose method after displaying the form.

Setup the problem...

When you click either button the Form2 will popup then you can click the OK
button. Once you click the button to close the form the messagebox will popup
and state "Form2 - 0: False". Close the messagebox. At this point I expect
the Form2 variable to be dispose and irrelevant.

Enter the problem...

Now if you click Form1's input panel you will get a message from the Form2
that states "Form2 - 0: True". At this point you shouldn't be running any
code in Form2...

Code below...

public partial class Form1 : Form
{
protected int count;

public Form1()
{
InitializeComponent();

count = 0;
}

private void button1_Click(object sender, EventArgs e)
{
Form2 dlg = new Form2(count++);

try
{
dlg.ShowDialog();
}
finally
{
dlg.Dispose();
}
}

private void button2_Click(object sender, EventArgs e)
{
Form2 dlg = new Form2(count++);
dlg.ShowDialog();
}

}

public partial class Form2 : Form
{
protected int _id;

public Form2(int ID)
{
InitializeComponent();

_id = ID;
label1.Text = "Form2: " + _id.ToString();

inputPanelForm2.Enabled = true;
}

private void inputPanelForm2_EnabledChanged(object sender, EventArgs
e)
{
MessageBox.Show("Form2 - " + _id.ToString() + ": " +
inputPanelForm2.Enabled.ToString());
}

private void Form2_Closing(object sender, CancelEventArgs e)
{
inputPanelForm2.Enabled = false;
}
}

You need to explicitly call InputPanelForm2.Dispose in your
Form2_Closing method.

Calling Dispose() on a control (including forms) does not
automatically call Dispose() on the contained components.
 
G

Guest

Is that just for InputPanel? or all controls... I was under the impression
that the framework did that? Does this apply to the full framework?
 
G

Guest

The framework Disposes all Controls in the Form's Controls collection.
Components (like the InputPanel) and anything not in the Controls collection
must be manually Disposed.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com
 
G

Guest

I understand but just looking at the MSDN how would I know to dispose of this
specific toolbox item? What other "controls" need to be manually disposed of?
Is there a list?
 
G

Guest

There's no "list" as there can't be - whether a Control needs to be disposed
manually or not depends on how it's used in the application. When a Form is
disposed, the Controls collection is iterated and each item in it is
Disposed. Components are never added to the Controls collection, so
anything that is a Component must be manually disposed. If you create a
Control, but don't add it to the Controls collection (maybe you use it as a
sync root, maybe for marshaling Invoke calls, whatever) then it would need
manual Disposal as well.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top