这是内存泄漏吗?
private void Process()
{
for (; ; )
{
// local variable
RemoteClient remoteClient = new RemoteClient(..);
// subscription without unsubscription
remoteClient.BadClient += new EventHandler(remoteClient_BadClient);
}
..
}
public class RemoteClient
{
...
public event EventHandler BadClient;
}
最佳答案
这取决于 RemoteClient 类中的其他内容。如果没有要dispose
的对象,则不会发生内存泄漏。如果有任何对象具有 IDisposable
内容,则需要继承 ÌDisposable` 并销毁这些对象。
我认为删除处理程序并退出循环对您来说也不是什么新鲜事。
因为客户端听起来像一个 Web 服务,所以了解一下所谓的异步线程可能很重要。 .NET: Do I need to keep a reference to WebClient while downloading asynchronously?
此外,如果整个事情变得更加复杂,检查对象状态也很重要。
关于c# - 这个事件处理代码会导致内存泄漏吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7649745/