为什么对事件处理程序的这种弱引用被垃圾收集

本文关键字:引用 事件处理 程序 为什么 | 更新日期: 2023-09-27 18:00:05

为什么在下面的示例中使用垃圾回收事件处理程序
我希望在垃圾收集之后收到事件,但事实并非如此
问题与WeakEventManager无关。

class WeakEventTest
{
    public static void Run() {
        EventConsumer ec = new EventConsumer();
        WeakEvent<EventArgs> weakEvent = new WeakEvent<EventArgs>();
        EventHandler<EventArgs> eh = ec.HandleEvent;
        weakEvent += new WeakReference<EventHandler<EventArgs>>(ec.HandleEvent);
        Console.WriteLine("Calling trigger");
        weakEvent.Trigger(null, EventArgs.Empty);
        Console.WriteLine("Calling System.GC.Collect");
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.WaitForFullGCComplete();
        GC.Collect();
        // event handler not called here
        Console.WriteLine("Calling trigger");
        weakEvent.Trigger(null, EventArgs.Empty);
    }

}
class EventConsumer
{
    public void HandleEvent(object obj, EventArgs args)
    {
        Console.WriteLine("EventReceived");
    }
}
public class WeakEvent<T>
{
    private List<WeakReference<EventHandler<T>>> referenceList = new List<WeakReference<EventHandler<T>>>();
    private EventHandler<T> handler = null;
    public static WeakEvent<T> operator +(WeakEvent<T> a, EventHandler<T> b)
    {
        lock (a.referenceList)
        {
            a.handler += b;
        }
        return a;
    }
    public static WeakEvent<T> operator +(WeakEvent<T> a, WeakReference<EventHandler<T>> b)
    {
        lock (a.referenceList)
        {
            a.referenceList.Add(b);
        }
        return a;
    }
    public static WeakEvent<T> operator -(WeakEvent<T> a, EventHandler<T> b)
    {
        lock (a.referenceList)
        {
            for (int i = a.referenceList.Count - 1; i >= 0; i--)
            {
                WeakReference<EventHandler<T>> wr = a.referenceList[i];
                EventHandler<T> target;
                if (!wr.TryGetTarget(out target))
                {
                    a.referenceList.RemoveAt(i);
                    continue;
                }
                if (Object.ReferenceEquals(target, b))
                {
                    a.referenceList.RemoveAt(i);
                    break;
                }
            }
            a.handler -= b;
        }
        return a;
    }
    public void Trigger(object obj, T args)
    {
        lock (referenceList)
        {
            for (int i = referenceList.Count - 1; i >= 0; i--)
            {
                WeakReference<EventHandler<T>> wr = referenceList[i];
                EventHandler<T> target;
                if (!wr.TryGetTarget(out target))
                {
                    referenceList.RemoveAt(i);
                    continue;
                }
                target(obj, args);
            }
            if (handler != null)
            {
                handler(obj, args);
            }
        }
    }
    public WeakEvent<T> AddWeakHandler(EventHandler<T> b)
    {
        lock (referenceList)
        {
            referenceList.Add(new WeakReference<EventHandler<T>>(b));
        }
        return this;
    }

控制台中的输出为:
调用触发器
事件接收
调用System.GC.Collect
调用触发器
-->这里是EventReceived

在下面的简单示例中,引用不是垃圾收集的,并且按预期工作。

class Program
{
    static void Main(string[] args)
    {
        var ec = new EventConsumer();
        var wr = new WeakReference<EventHandler<EventArgs>>(ec.EventReceived);
        EventHandler<EventArgs> target;
        if (wr.TryGetTarget(out target))
        {
            Console.WriteLine("Raising event");
            target(null, EventArgs.Empty);
        }
        Console.WriteLine("Calling System.GC.Collect");
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.WaitForFullGCComplete();
        GC.Collect();
        EventHandler<EventArgs> target2;
        if (wr.TryGetTarget(out target2))
        {
            Console.WriteLine("Raising event");
            target2(null, EventArgs.Empty);
        }
        Console.ReadKey();
    }
}
public class EventConsumer
{
    public void EventReceived(object obj, EventArgs args)
    {
        Console.WriteLine("EventReceived");
    }
}

为什么对事件处理程序的这种弱引用被垃圾收集

第一个结果是预期的结果:您只使用弱引用引用ec,因此没有理由不收集它。

第二个例子更微妙:ec之所以保持活动状态,是因为你在target上保留了一个引用(它反过来又引用ec)。只要清除该引用,您就会观察到与第一个示例相同的行为:

class Program
{
    static void Main(string[] args)
    {
        var ec = new EventConsumer();
        var wr = new WeakReference<EventHandler<EventArgs>>(ec.EventReceived);
        EventHandler<EventArgs> target;
        if (wr.TryGetTarget(out target))
        {
            Console.WriteLine("Raising event");
            target(null, EventArgs.Empty);
        }
        // Clear the reference
        target = null;
        Console.WriteLine("Calling System.GC.Collect");
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.WaitForFullGCComplete();
        GC.Collect();
        EventHandler<EventArgs> target2;
        if (wr.TryGetTarget(out target2))
        {
            Console.WriteLine("Raising event");
            target2(null, EventArgs.Empty);
        }
        Console.ReadKey();
    }
}
public class EventConsumer
{
    public void EventReceived(object obj, EventArgs args)
    {
        Console.WriteLine("EventReceived");
    }
}

请注意,您需要在发布模式下进行编译。在调试模式下,对象一直保持活动状态,直到方法结束(即使它们不再被引用),以使调试更容易。