J
Jay Dee
Object garbage collection of events
I can not help wondering how the garbage collection system handles
events.
If I release all references to an object that has a void that an event
somewhere is using I then have no access to the class holding the void
so it should be garbage collected.
But if an event holds a reference to a void of the class that I have
no reference to douse the class stay in memory because something
refers to it or is that reference removed from the event.
Basically do I need to remove all event calls before releasing an
object or are they removed automatically.
I hope someone understands what I mean.
This code thaw incomplete hopefully helps explain what I mean.
Thank you all
Jay Dee
<code>
static class Program
{
static void Main()
{
Form1 form = new Form1();
form.myClass = new MyClass();
form.MyEvent += new EventHandler(form.myClass.MyEventCall);
form.myClass = null;
Application.Run(form);
}
}
public class Form1 : System.Windows.Forms.Form
{
MyClass myClass;
public event System.EventHandler MyEvent;
public void OnMyEvent(EventArgs e)
{
if (this.MyEvent != null)
{
this.MyEvent(this, e);
}
}
}
public class MyClass
{
public void MyEventCall(object sender, EventArgs e)
{
// do somthing.
}
}
</code>
I can not help wondering how the garbage collection system handles
events.
If I release all references to an object that has a void that an event
somewhere is using I then have no access to the class holding the void
so it should be garbage collected.
But if an event holds a reference to a void of the class that I have
no reference to douse the class stay in memory because something
refers to it or is that reference removed from the event.
Basically do I need to remove all event calls before releasing an
object or are they removed automatically.
I hope someone understands what I mean.
This code thaw incomplete hopefully helps explain what I mean.
Thank you all
Jay Dee
<code>
static class Program
{
static void Main()
{
Form1 form = new Form1();
form.myClass = new MyClass();
form.MyEvent += new EventHandler(form.myClass.MyEventCall);
form.myClass = null;
Application.Run(form);
}
}
public class Form1 : System.Windows.Forms.Form
{
MyClass myClass;
public event System.EventHandler MyEvent;
public void OnMyEvent(EventArgs e)
{
if (this.MyEvent != null)
{
this.MyEvent(this, e);
}
}
}
public class MyClass
{
public void MyEventCall(object sender, EventArgs e)
{
// do somthing.
}
}
</code>