未注册的事件处理程序导致内存泄漏 [英] Unregistered event handlers cause memory leak

查看:135
本文介绍了未注册的事件处理程序导致内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持认为有内存泄漏的Web应用程序。

I'm maintaining a web application that has a memory leak.

有这么上了加,这样,当项目的日期被改变集合可以重新排序每一个项目注册事件处理程序的集合。看来,此事件处理程序的罪魁祸首。

There's a collection that registers an event handler on each item that's added so that the collection can re-sort when the item's date is changed. It appears that this event handler is the culprit.

此应用程序的业务层是相当复杂的,因此保持集合及其在内存中的项目一拖再拖了一堆其他对象的吧。

The business layer for this application is quite complicated, so keeping the collection and its items in memory drags a bunch of other objects with it.

我已经实现了IDisposable关于收集和去除的Dispose方法的事件处理程序:

I've implemented IDisposable on the collection and removed the event handlers in the Dispose method:

p.OnPunchDateChanged -= this.OnPunchDateChanged;

但是,实现了IDisposable没有帮助,因为我不能换到集合中的所有参考使用或try / catch块。此集合应用程序使用的部分,我没有控制权。

However, implementing IDisposable doesn't help since I can't wrap all the references to the collection in using or try/catch blocks. This collection is used by portions of the application that I don't have control over.

我怎样才能清除这些事件处理程序来解决此内存泄漏?

How can I clear these event handlers to resolve this memory leak?

推荐答案

首先,只是为了证明这一点,尝试登录的添加和删除事件以一个简单的文本文件。然后,检查有多少人加入VS去除。

First off, just to prove the point, try logging the adding and removal of events to a simple text file. Then, check how many were added vs removed.

听起来好像有一个bug某处这是不是在所有情况下注销事件的业务逻辑。

It sounds as if there is a bug somewhere in the business logic which is not unregistering the event in all circumstances.

这篇关于未注册的事件处理程序导致内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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