R
Robert Conde
Consider the test app below. While I hit the breakpoint in the finalizer in
DataClass, I never hit the one in Form2. How is that possible? Won't Form2
hold a reference to DataClass until it's finalized, therefore preventing
finalization of DataClass? Or can GC say "no one has a reference to Form2,
and Form2 is ready for finalization, so we can finalize DataClass and ignore
Form2 for now because it's small". I could maybe understand that - but I
NEVER hit the breakpoint in the Form2 finalizer, even on application
shutdown. Kinda makes it hard to know if I have dangling references.
using System;
using System.Windows.Forms;
namespace FinalizerTestWinforms
{
static class Test
{
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
class Form1 : System.Windows.Forms.Form
{
Timer timer = new Timer();
public Form1()
{
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
Form2 newForm = new Form2();
newForm.Show();
newForm.Dispose();
newForm = null;
GC.Collect();
}
}
class Form2 : System.Windows.Forms.Form
{
DataClass data = new DataClass();
~Form2()
{
int five = 5;//put breakpoint here
}
}
class DataClass
{
double[] data = new double[5000000];//to add memory pressure...
~DataClass()
{
int five = 5;//put breakpoint here...
}
}
}
DataClass, I never hit the one in Form2. How is that possible? Won't Form2
hold a reference to DataClass until it's finalized, therefore preventing
finalization of DataClass? Or can GC say "no one has a reference to Form2,
and Form2 is ready for finalization, so we can finalize DataClass and ignore
Form2 for now because it's small". I could maybe understand that - but I
NEVER hit the breakpoint in the Form2 finalizer, even on application
shutdown. Kinda makes it hard to know if I have dangling references.
using System;
using System.Windows.Forms;
namespace FinalizerTestWinforms
{
static class Test
{
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
class Form1 : System.Windows.Forms.Form
{
Timer timer = new Timer();
public Form1()
{
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
Form2 newForm = new Form2();
newForm.Show();
newForm.Dispose();
newForm = null;
GC.Collect();
}
}
class Form2 : System.Windows.Forms.Form
{
DataClass data = new DataClass();
~Form2()
{
int five = 5;//put breakpoint here
}
}
class DataClass
{
double[] data = new double[5000000];//to add memory pressure...
~DataClass()
{
int five = 5;//put breakpoint here...
}
}
}