在对象的孤立之前,是否需要删除事件订阅? [英] Do I need to remove event subscriptions from objects before they are orphaned?

查看:92
本文介绍了在对象的孤立之前,是否需要删除事件订阅?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的软件有两个对象实例,其中之一是订阅对方的事件。在孤儿被垃圾收集者清理之前,我需要取消订阅吗?还有还有其他原因我应该清除事件关系?如果订阅的对象是孤立的,但订阅者不是,或者反之亦然?

解决方案

是的。事件发布者持有对象的引用,并将阻止它们被垃圾收集。



我们来看一个例子来看看会发生什么。我们有两堂课一个暴露一个事件,另一个消耗它:

  class ClassA 
{
public event EventHandler Test;
〜ClassA()
{
Console.WriteLine(A being collected);
}
}
class ClassB
{
public ClassB(ClassA instance)
{
instance.Test + = new EventHandler(instance_Test) ;
}

〜ClassB()
{
Console.WriteLine(B正在收集);
}

void instance_Test(object sender,EventArgs e)
{
//此空间有意留空
}
}

请注意ClassB如何不存储对ClassA实例的引用;它只是挂起一个事件处理程序。



现在,我们来看看如何收集对象。方案1:

  ClassB temp = new ClassB(new ClassA()); 
Console.WriteLine(Collect 1);
GC.Collect();
Console.ReadKey();
temp = null;
Console.WriteLine(Collect 2);
GC.Collect();
Console.ReadKey();

我们创建一个ClassB实例,并通过临时变量保持对它的引用。它通过了一个新的ClassA实例,我们不在任何地方存储它的引用,所以在ClassB构造函数完成之后立即超出范围。当ClassA超出范围时,我们有垃圾收集器运行一次,一旦ClassB超出范围。输出:

 收集1 
A正在收集
收集2
B正在收集

情景2:

 code> ClassA temp = new ClassA(); 
ClassB temp2 = new ClassB(temp);
temp2 = null;
Console.WriteLine(Collect 1);
GC.Collect();
Console.ReadKey();
temp = null;
Console.WriteLine(Collect 2);
GC.Collect();
Console.ReadKey();

创建了一个新的ClassA实例,并将其引用存储在temp变量中。然后创建一个新的ClassB实例,将ClassA实例传递给temp,并在temp2中存储对它的引用。然后我们将temp2设置为null,使ClassB实例超出范围。如前所述,每个实例超出范围后,我们都将垃圾收集器运行。输出:

 收集1 
收集2
B正在收集
A正在收集

所以,得出结论;如果暴露事件的实例超出范围,则无论是否连接有事件处理程序,它都可用于垃圾回收。如果具有事件处理程序的实例与另一个实例中的事件挂钩,则在事件处理程序分离之前,或者附加事件处理程序的实例可用于进行垃圾回收时,它将不可用于垃圾回收。 / p>

If my software has two object instances, one of which is subscribed to the events of the other. Do I need to unsubscribe them from one another before they are orphaned for them to be cleaned up by the garbage collector? Or is there any other reason why I should clear the event relationships? What if the subscribed to object is orphaned but the subscriber is not, or vise versa?

解决方案

Yes you do. The event publishers are holding references to the objects, and would prevent them from being garbage collected.

Let's look at an example to see what happens. We have two classes; one exposes an event, the other consumes it:

class ClassA
{
    public event EventHandler Test;
    ~ClassA()
    {
        Console.WriteLine("A being collected");
    }
}
class ClassB
{
    public ClassB(ClassA instance)
    {
        instance.Test += new EventHandler(instance_Test);
    }

    ~ClassB()
    {
        Console.WriteLine("B being collected");
    }

    void instance_Test(object sender, EventArgs e)
    {
        // this space is intentionally left blank
    }
}

Note how ClassB does not store a reference to the ClassA instance; it merely hooks up an event handler.

Now, let's see how the objects are collected. Scenario 1:

ClassB temp = new ClassB(new ClassA());
Console.WriteLine("Collect 1");
GC.Collect();
Console.ReadKey();
temp = null;
Console.WriteLine("Collect 2");
GC.Collect();
Console.ReadKey();

We create a ClassB instance and hold a reference to it through the temp variable. It gets passed a new instance of ClassA, where we do not store a reference to it anywhere, so it goes out of scope immediately after the ClassB constructor is done. We have the garbage collector run once when ClassA has gone out of scope, and once when ClassB as gone out of scope. The output:

Collect 1
A being collected
Collect 2
B being collected

Scenario 2:

ClassA temp = new ClassA();
ClassB temp2 = new ClassB(temp);
temp2 = null;
Console.WriteLine("Collect 1");
GC.Collect();
Console.ReadKey();
temp = null;
Console.WriteLine("Collect 2");
GC.Collect();
Console.ReadKey();

A new instance of ClassA is created and a reference to it is stored in the temp variable. Then a new instance of ClassB is created, getting the ClassA instance in temp passed to it, and we store a reference to it in temp2. Then we set temp2 to null, making the ClassB instance going out of scope. As before, we have the garbage collector run after each instance has gone out of scope. The output:

Collect 1
Collect 2
B being collected
A being collected

So, to conclude; if the instance that exposes an event goes out of scope, it becomes available for garbage collection, regardless of whether there are event handlers hooked up or not. If an instance that has an event handler hooked up to an event in another instance, it will not be available for garbage collection until either the event handler is detached, or the instance to which the event handler is attached becomes available for garbage collection.

这篇关于在对象的孤立之前,是否需要删除事件订阅?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆