【发布时间】:2011-11-02 14:42:23
【问题描述】:
我正在对使用 ILSpy 在 vb.net 中编写的旧 Windows 服务进行逆向工程。我想用 C# 重写旧服务。原始服务泄漏内存。
当我查看源代码时,服务类中有以下声明:
private static List<WeakReference> __ENCList = new List<WeakReference>();
这个列表只在构造函数中使用如下(imineRun是服务类):
List<WeakReference> _ENCList = imineRun.__ENCList;
Monitor.Enter(_ENCList);
try
{
imineRun.__ENCList.Add(new WeakReference(this));
}
finally
{
Monitor.Exit(_ENCList);
}
这个列表会导致内存泄漏吗?是否需要此列表,其目的是什么?
【问题讨论】:
-
而
imineRun.__ENCList没有在其他地方使用? -
@sixlettervariables 只在构造函数中使用
-
弱引用是一个可以但不必在 GC 收集时间删除的项目。我不知道代码是公认的做法还是糟糕的编码。
-
@NicholasButler 我可以...我不确定它是否仍然免费并且我没有安装它。我很确定它会给出相同的结果。
-
Reflector 不再免费(抱怨)。一个很好的选择是 dotPeek。但我非常怀疑反编译后的输出是否会有任何重大差异。
标签: c# multithreading windows-services weak-references