问题描述
事件的对象垃圾收集
我不禁想知道垃圾收集系统如何处理
事件。
如果我释放对某个对象的所有引用,该对象具有某个事件
某个地方正在使用的空格,那么我就无法访问持有该空格的类
所以它应该是垃圾收集。
但是如果一个事件持有对我所拥有的类的空白的引用
没有引用这个类留在内存中因为某事物
是指它或者是从事件中删除的引用。
基本上我需要在发布之前删除所有事件调用
对象或是否自动删除。
我希望有人理解我的意思。
这段代码解冻不完整有希望帮助解释我是什么意思。
谢谢大家
Jay Dee
< code>
静态类程序
{
static void Main()
{
Form1 form = new Form1();
form.myClass = new MyClass();
form.MyEvent + = new EventHandler(form.myClass.MyEventCall);
form.myClass = null;
Application.Run(form);
}
}
公共类Form1:System.Windows .Forms.Form
{
MyClass myClass;
公共事件System.EventHandler MyEvent;
public void OnMyEvent(EventArgs e)
{
if(this.MyEvent!= null)
{
this.MyEvent(this,e);
}
}
}
公共类MyClass
{
public void MyEventCall(object sender,EventArgs e)
{
//做某事。
}
}
< / code>
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>
推荐答案
你是什么意思无效?如果你的意思是方法或代表,那么你就会严重滥用void这个词。
What do you mean by "void"? If you mean a method or delegate, then you''re
severely abusing the word "void".
假设您询问在您发布的代码中,
MyClass的实例是否符合表格的垃圾收集条件。执行myClass = null"
,答案为no,事件处理程序未自动删除
,因此MyClass实例仍然可以访问。
Pete
Assuming you are asking whether, in the code you posted, the instance of
MyClass becomes eligible for garbage collection when "form.myClass = null"
is executed, the answer is "no", the event handler is not removed
automatically and so the MyClass instance remains reachable.
Pete
间接地,他们这样做。 />
Indirectly, they do.
确实如此。无论代表引用什么,事件都会保留对订阅该事件的任何委托的
引用。如果该委托是从实例方法构造的
,那么它包含对所使用的
类实例的引用,因此该事件也隐含地包含了
引用。
It does. Regardless of what the delegate references, an event retains a
reference to any delegate subscribed to the event. If that delegate was
constructed from an instance method, then it includes a reference to the
class instance used, and so the event implicitly also includes that
reference.
事件与对象中的任何其他字段没有区别。
实际上没有根,如类实例必须由
引用其他东西(例如,通过堆栈上的局部变量,
_would_是根),但只要对象本身是可以到达的,所以也是如此。
是其字段引用的任何内容,包括任何事件。
Pete
There''s no difference between an event and any other field in the object.
None are actually "roots", as the class instance must be referenced by
something else (for example, by a local variable on the stack, which
_would_ be a root), but as long as the object itself is reachable, so too
is anything referenced by its fields, including any events.
Pete
这篇关于事件的对象垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!