T
TPS
I've searched high and low, but can't seem to come across a solution
for garbage collection of my dynamically declared controls.
For testing I've created a small example:
I have two classes:
public class Foo
{
public Foo()
{
Debug.WriteLine("Foo ctor");
}
~Foo()
{
Debug.WriteLine("Desctructing Foo");
}
}
public class FooControl : Control
{
public FooControl()
{
Debug.WriteLine("FooControl ctor");
}
~FooControl()
{
Debug.WriteLine("Desctructing FooControl");
}
}
And I use them like this:
private void Form1_Load(object sender, EventArgs e)
{
List<Foo> list = new List<Foo>();
list.Add(new Foo());
list.Add(new Foo());
List<FooControl> list2 = new List<FooControl>();
list2.Add(new FooControl());
list2.Add(new FooControl());
}
This causes the output:
Foo ctor
Foo ctor
FooControl ctor
FooControl ctor
Which is correct...
Then I explicitly call for garbage collection:
GC.Collect();
Now this is output:
Desctructing Foo
Desctructing Foo
Not until I close the program using Application.Exit(); are the
FooControl objects destroyed, and outputs:
Desctructing FooControl
Desctructing FooControl
....
I've tried to create a regular .Net Framework Windows application with
exactly the same code, and here the FooControl objects are destroyed
correctly during garbage collection.
What can I do to make sure that my no longer referenced controls are
properly destroyed.
TPS.
for garbage collection of my dynamically declared controls.
For testing I've created a small example:
I have two classes:
public class Foo
{
public Foo()
{
Debug.WriteLine("Foo ctor");
}
~Foo()
{
Debug.WriteLine("Desctructing Foo");
}
}
public class FooControl : Control
{
public FooControl()
{
Debug.WriteLine("FooControl ctor");
}
~FooControl()
{
Debug.WriteLine("Desctructing FooControl");
}
}
And I use them like this:
private void Form1_Load(object sender, EventArgs e)
{
List<Foo> list = new List<Foo>();
list.Add(new Foo());
list.Add(new Foo());
List<FooControl> list2 = new List<FooControl>();
list2.Add(new FooControl());
list2.Add(new FooControl());
}
This causes the output:
Foo ctor
Foo ctor
FooControl ctor
FooControl ctor
Which is correct...
Then I explicitly call for garbage collection:
GC.Collect();
Now this is output:
Desctructing Foo
Desctructing Foo
Not until I close the program using Application.Exit(); are the
FooControl objects destroyed, and outputs:
Desctructing FooControl
Desctructing FooControl
....
I've tried to create a regular .Net Framework Windows application with
exactly the same code, and here the FooControl objects are destroyed
correctly during garbage collection.
What can I do to make sure that my no longer referenced controls are
properly destroyed.
TPS.