G
Guest
Hi, I am using Team Foundation Server's unit testing features and I have
discovered something that is causing me a bit of a problem: please take a
look at the following test code:
public void UnloadAnyControlTest()
{
Form form = new Form();
Panel panel = new Panel();
WeakReference panelRef = new WeakReference(panel);
panel.Dock = DockStyle.Fill;
// Below are the two lines of code which, if commented out,
// will permit this test to pass. Otherwise, it fails.
form.Controls.Add(panel);
form.Controls.Remove(panel);
GC.Collect();
GC.WaitForPendingFinalizers();
Assert.IsNotNull(panelRef.Target);
panel = null;
GC.Collect();
GC.WaitForPendingFinalizers();
Assert.IsNull(panelRef.Target);
}
Basically, I have some user controls that I developed and I was writing some
test code to ensure that they were properly cleaned up when I unloaded them.
I found that it wasn't happening, and I wrote the above test to see if I
could figure out what was going on.
Basically, if I create a panel and create a weak reference to it, then null
out (destroy) the only other reference I have to the panel, the garbage
collector will clean up the panel. If, however, in between there I add the
panel to a form and then remove it again, the panel will not be cleaned up!
Does anyone know why?
Can anyone suggest a solution or shed some light on this issue?
Thanks in advance,
-John
discovered something that is causing me a bit of a problem: please take a
look at the following test code:
public void UnloadAnyControlTest()
{
Form form = new Form();
Panel panel = new Panel();
WeakReference panelRef = new WeakReference(panel);
panel.Dock = DockStyle.Fill;
// Below are the two lines of code which, if commented out,
// will permit this test to pass. Otherwise, it fails.
form.Controls.Add(panel);
form.Controls.Remove(panel);
GC.Collect();
GC.WaitForPendingFinalizers();
Assert.IsNotNull(panelRef.Target);
panel = null;
GC.Collect();
GC.WaitForPendingFinalizers();
Assert.IsNull(panelRef.Target);
}
Basically, I have some user controls that I developed and I was writing some
test code to ensure that they were properly cleaned up when I unloaded them.
I found that it wasn't happening, and I wrote the above test to see if I
could figure out what was going on.
Basically, if I create a panel and create a weak reference to it, then null
out (destroy) the only other reference I have to the panel, the garbage
collector will clean up the panel. If, however, in between there I add the
panel to a form and then remove it again, the panel will not be cleaned up!
Does anyone know why?
Can anyone suggest a solution or shed some light on this issue?
Thanks in advance,
-John