c# - 事件监听器委托(delegate)中的自引用 (C#)

标签 c# delegates event-handling garbage-collection

这可能是语义问题,但也可能不是,所以我问:以下 2 个片段是否有明显差异?

public Parent()
{
    Child newChild = new Child();
    newChild.RequestSpecialEvent += (sender, e) =>
    {
        newChild.DoMagic();
    }
}

public Parent()
{
    Child newChild = new Child();
    newChild.RequestSpecialEvent += (sender, e) =>
    {
        ((Child)sender).DoMagic();
    }
}

明显的区别是选项 1 本身是一种自引用,而选项 2 对对象执行强制转换。性能方面,我预计 Actor 会更贵。

但是,我的理论是,在选项 1 中,从技术上讲,“父级”持有对“newChild”的引用(通过父级中定义的委托(delegate)),所以即使 newChild 消失(newChild = null 或其他)类似),newChild 对象不能被垃圾收集(gc'ed),因为 Parent 已经定义了一个仍然附加到它的委托(delegate)。 newChild 只能在 Parent 最终消失时被 gc'ed。

但是,在选项 2 中,Parent 永远不会创建对 newChild 的“硬引用”,因此当 newChild = null 发生时,它确实可以立即被 gc。

我更喜欢选项 1,因为它的简洁性和可读性,但担心选项 2 会更好。想法?我的理论是正确的还是错误的?是否有替代或更优选的方法(具有合理的推理)来声明与父/子类的相同事件监听器关系?

对@StriplingWarrior 的回应:

关于垃圾回收,我仍然有点怀疑。委托(delegate)引用了 newChild,所以对我来说,似乎 newChild 在委托(delegate)离开之前不能离开。现在,如果 newChild 离开,委托(delegate)将离开……但 newChild 仍然不能离开,直到委托(delegate)离开!似乎是圆形的(几乎)。似乎必须发生这种情况:

//newChild = null;
//This alone won't truly free up the newChild object because the delegate still
//points to the newChild object.

//Instead, this has to happen
newChild.RequestSpecialEvent = null; //destroys circular reference to newChild
newChild = null; //truly lets newChild object be gc'd

或者说“newChild = null;”只有 newChild.RequestSpecialEvent 停止指向委托(delegate),这允许委托(delegate)离开,然后允许 newChild 离开?也许我只是说服自己接受了你的回答。 :)

最佳答案

你的想法似乎很准确,除了我很确定 newChild 仍然可以在选项 1 中被垃圾收集,因为引用它的委托(delegate)本身只被处理程序引用在 newChild 本身上。

这两个代码段在功能上是等效的,由于委托(delegate)中的额外引用,选项 1 使用的内存稍微更多,但调用时速度稍快一些,因为它避免了强制转换。两种方式的区别都可以忽略不计,我建议使用感觉最干净的那种(#1,如果你问我的话)。

我唯一会选择类似#2 的情况是,如果您想将同一个委托(delegate)应用于多个控件。在这种情况下,这最终会使用更少的内存:

var handler = new RequestSpecialEventHandler((sender, e) =>
    {
        ((Child)sender).DoMagic();
    });
foreach(var child in children)
{
    child.RequestSpecialEvent += handler;
}

还有一个需要注意的警告是,由于选项 #1 指的是 newChild 变量,如果该变量的值稍后在方法中发生变化,则在处理程序被调用。例如,在这个例子中:

foreach(var child in children)
{
    child.RequestSpecialEvent += (sender, e) =>
        {
            // BEWARE: Don't do this! Modified closure!
            child.DoMagic();
        };
}

... 任何时候这些 child 中的任何一个触发事件,“魔术”将执行 N 次仅对 children 集合中的最后一个 child

关于c# - 事件监听器委托(delegate)中的自引用 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6779760/

相关文章:

ios - 在新 Storyboard中将值传递给 UIViewController - Swift

jquery - Bootstrap 按钮下拉菜单中的 Bootstrap 日期选择器

c# - 打破委托(delegate)内的循环

c# - 我的应用程序不会在 .net 2.0 下运行,适用于 3.5

c# - 如何在 C#/.NET 4.0 中编写带有委托(delegate)参数的方法?

ios - 一个对象的变化应该影响同一类的其他对象

event-handling - 如何绑定(bind)到 jQuery UI 选项卡事件单击/选择/事件?

node.js - Node.js 什么时候阻塞?

C# 线程模式 - 这是个好主意吗?

c# - 在 asp.net 中记录异常