WeakReference 和事件处理

发布于 2024-07-07 04:18:42 字数 137 浏览 8 评论 0原文

如果该事件是唯一保存引用的事件并且我们需要对对象进行垃圾收集,那么通过 WeakReference 实现事件处理是否是一个好习惯?

作为对此的一个论据:

人们说,如果您订阅了某些内容,那么您有责任取消订阅,并且您应该这样做。

Is it a good practice to implement event handling through WeakReference if that event is the only thing holding the reference and that we would need the object to be garbage collected?

As an argument to this:

Folks say that if you subscribe to something it’s your responsibility to unsubscribe and you should do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

因为看清所以看轻 2024-07-14 04:18:42

尽可能养成取消订阅事件的习惯是件好事,但有时没有明显的“清理”方法可以完成此操作。 我们最近发布了一篇关于此主题的博客文章; 它包含一些方法,可以轻松地使用 Wea​​kReference 订阅事件。

It is good to get in the habit of unsubscribing from events when you can, but sometimes there isn't an obvious "cleanup" method where it can be done. We recently posted a blog article on this subject; it includes methods that make it easy to subscribe to an event with a WeakReference.

不疑不惑不回忆 2024-07-14 04:18:42

弱委托模式是 CLR 中应该存在的东西。 正常事件表现出“当你还活着的时候通知我”的语义,而我们通常需要“当我还活着的时候通知我”。 仅仅在 WeakReference 上使用 delegate 是错误的,因为 delegate 也是一个对象,即使接收者仍然活着并且有传入引用,delegate 本身也只会被所述 WeakReference 引用,并且会立即被收集。 请参阅这篇旧文章 实现示例。

Weak delegate pattern is something that should be there in CLR. Normal events exhibit "notify me while you are alive" semantics, while often we need "notify me while I'm alive". Just having delegate on WeakReference is wrong, because delegate is an object too and even when recepient is still alive and have incoming references, delegate itself is only being referenced by said WeakReference and will be collected instantly. See this old post for an example of implementation.

您的好友蓝忘机已上羡 2024-07-14 04:18:42

弱引用本身并不能解决问题,因为委托持有引用。 在 Prism (www.microsoft.com/compositewpf) 附带的复合应用程序库中,有一个 WeakDelegate 类,您可以从源代码中提取该类。 WeakDelegate 基本上使用反射,仅创建一个委托一段时间,然后释放它,因此不持有任何指针。 在 CAL 中,它由 EventAggregator 类使用,但您可以随意将其剥离以供自己使用,因为它位于 MS-PL 下。

Weak references in their own right, don't solve the problem as the delegate holds the reference. In the Composite Application Library which ships with Prism (www.microsoft.com/compositewpf) there is a WeakDelegate class that you could pull from the source. The WeakDelegate basically ues reflection and creates a delegate only for a moment in time and then releases it, thereby no holding any pointers. Within CAL it is used by the EventAggregator class, but you are free to rip it out for your own usage as it is under MS-PL.

老娘不死你永远是小三 2024-07-14 04:18:42

虽然您的建议解决了一组问题(事件引用管理和内存泄漏预防),但它可能会带来一组新的问题。

我看到的一个问题是,在事件处理过程中,如果源对象被垃圾收集(因为它仅由弱引用保存),则任何访问源对象的代码都将导致空引用异常。 您可以认为事件处理程序要么不应该访问源对象,要么它必须具有强引用,但可以认为这可能是比您首先尝试解决的问题更糟糕的问题。

While what you suggest solves one set of problems (event reference management and memory leak prevention), it is likely to open up a new set of problems.

One problem I can see is during event handling process if the source object is garbage collected (as it was only held with a weak reference), any code that access the source object will result in null reference exception. You can argue that the event handler should either not access the source object or it must have a strong reference, but it can be argued that this could be a worse problem than the one you are trying to solve in the first place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文