gpt4 book ai didi

c# - 弱引用已死

转载 作者:太空狗 更新时间:2023-10-29 18:08:20 25 4
gpt4 key购买 nike

我正在玩弄一个事件聚合器,在我的订阅者对象中使用弱引用方法我希望处理事件。

订阅 时,弱引用 成功创建,我的订阅者 集合相应更新。但是,当我尝试发布 事件时,弱引用 已被 GC 清除。下面是我的代码:

public class EventAggregator
{
private readonly ConcurrentDictionary<Type, List<Subscriber>> subscribers =
new ConcurrentDictionary<Type, List<Subscriber>>();

public void Subscribe<TMessage>(Action<TMessage> handler)
{
if (handler == null)
{
throw new ArgumentNullException("handler");
}

var messageType = typeof (TMessage);
if (this.subscribers.ContainsKey(messageType))
{
this.subscribers[messageType].Add(new Subscriber(handler));
}
else
{
this.subscribers.TryAdd(messageType, new List<Subscriber> {new Subscriber(handler)});
}
}

public void Publish(object message)
{
if (message == null)
{
throw new ArgumentNullException("message");
}

var messageType = message.GetType();
if (!this.subscribers.ContainsKey(messageType))
{
return;
}

var handlers = this.subscribers[messageType];
foreach (var handler in handlers)
{
if (!handler.IsAlive)
{
continue;
}

var actionType = handler.GetType();
var invoke = actionType.GetMethod("Invoke", new[] {messageType});
invoke.Invoke(handler, new[] {message});
}
}

private class Subscriber
{
private readonly WeakReference reference;

public Subscriber(object subscriber)
{
this.reference = new WeakReference(subscriber);
}

public bool IsAlive
{
get
{
return this.reference.IsAlive;
}
}
}
}

我通过以下方式订阅发布:

ea.Subscribe<SomeEvent>(SomeHandlerMethod);
ea.Publish(new SomeEvent { ... });

我可能正在做一些非常愚蠢的事情,说我很难看到我的错误。

最佳答案

这里有一些问题(其他人已经提到了其中的一些),但最主要的是编译器正在创建一个没有人持有强引用的新委托(delegate)对象。编译器采用

ea.Subscribe<SomeEvent>(SomeHandlerMethod);

并插入适当的委托(delegate)转换,有效地给出:

ea.Subscribe<SomeEvent>(new Action<SomeEvent>(SomeHandlerMethod));

然后这个委托(delegate)被收集(只有你的 WeakReference 指向它)并且订阅被清理。

您还存在线程安全问题(我假设您为此目的使用了 ConcurrentDictionary)。具体来说,对 ConcurrentDictionaryList 的访问根本不是线程安全的。列表需要锁定,您需要正确使用 ConcurrentDictionary 进行更新。例如,在您当前的代码中,TryAdd block 中可能有两个单独的线程,其中一个将失败导致订阅丢失。

我们可以解决这些问题,但让我概述一下解决方案。由于那些自动生成的委托(delegate)实例,在 .Net 中实现弱事件模式可能很棘手。取而代之的是在 WeakReference 中捕获委托(delegate)的 Target,如果它有的话(如果它是静态方法,则可能没有)。然后,如果该方法是一个实例方法,我们将构造一个等效的 Delegate,它没有 Target,因此不会有强引用。

using System.Collections.Concurrent;
using System.Diagnostics;

public class EventAggregator
{
private readonly ConcurrentDictionary<Type, List<Subscriber>> subscribers =
new ConcurrentDictionary<Type, List<Subscriber>>();

public void Subscribe<TMessage>(Action<TMessage> handler)
{
if (handler == null)
throw new ArgumentNullException("handler");

var messageType = typeof(TMessage);
var handlers = this.subscribers.GetOrAdd(messageType, key => new List<Subscriber>());
lock(handlers)
{
handlers.Add(new Subscriber(handler));
}
}

public void Publish(object message)
{
if (message == null)
throw new ArgumentNullException("message");

var messageType = message.GetType();

List<Subscriber> handlers;
if (this.subscribers.TryGetValue(messageType, out handlers))
{
Subscriber[] tmpHandlers;
lock(handlers)
{
tmpHandlers = handlers.ToArray();
}

foreach (var handler in tmpHandlers)
{
if (!handler.Invoke(message))
{
lock(handlers)
{
handlers.Remove(handler);
}
}
}
}
}

private class Subscriber
{
private readonly WeakReference reference;
private readonly Delegate method;

public Subscriber(Delegate subscriber)
{
var target = subscriber.Target;

if (target != null)
{
// An instance method. Capture the target in a WeakReference.
// Construct a new delegate that does not have a target;
this.reference = new WeakReference(target);
var messageType = subscriber.Method.GetParameters()[0].ParameterType;
var delegateType = typeof(Action<,>).MakeGenericType(target.GetType(), messageType);
this.method = Delegate.CreateDelegate(delegateType, subscriber.Method);
}
else
{
// It is a static method, so there is no associated target.
// Hold a strong reference to the delegate.
this.reference = null;
this.method = subscriber;
}

Debug.Assert(this.method.Target == null, "The delegate has a strong reference to the target.");
}

public bool IsAlive
{
get
{
// If the reference is null it was a Static method
// and therefore is always "Alive".
if (this.reference == null)
return true;

return this.reference.IsAlive;
}
}

public bool Invoke(object message)
{
object target = null;
if (reference != null)
target = reference.Target;

if (!IsAlive)
return false;

if (target != null)
{
this.method.DynamicInvoke(target, message);
}
else
{
this.method.DynamicInvoke(message);
}

return true;
}
}
}

和一个测试程序:

public class Program
{
public static void Main(string[] args)
{
var agg = new EventAggregator();
var test = new Test();
agg.Subscribe<Message>(test.Handler);
agg.Subscribe<Message>(StaticHandler);
agg.Publish(new Message() { Data = "Start test" });
GC.KeepAlive(test);

for(int i = 0; i < 10; i++)
{
byte[] b = new byte[1000000]; // allocate some memory
agg.Publish(new Message() { Data = i.ToString() });
Console.WriteLine(GC.CollectionCount(2));
GC.KeepAlive(b); // force the allocator to allocate b (if not in Debug).
}

GC.Collect();
agg.Publish(new Message() { Data = "End test" });
}

private static void StaticHandler(Message m)
{
Console.WriteLine("Static Handler: {0}", m.Data);
}
}

public class Test
{
public void Handler(Message m)
{
Console.WriteLine("Instance Handler: {0}", m.Data);
}
}

public class Message
{
public string Data { get; set; }
}

关于c# - 弱引用已死,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20169296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com