识别 IDisposable 对象
我必须检查其他人编写的存在内存泄漏的代码。 现在我正在搜索一次性对象以使用 using 语句将它们包围起来,我想知道是否有一种快速方法可以告诉您声明的所有一次性对象。我的意思是像 resharper 或其他 Visual Studio 插件之类的东西。
谢谢。
i have to review a code made by some other person that has some memory leaks. Right now i'm searching the disposable objects to enclause them with the using statement and i would like to know if there is a quick way that tells you all the disposable objects declared in. I mean something like resharper or another visual studio plugin.
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另外,根据您是否使用这样的系统,如果您使用 IoC 容器,在通过接口将服务返回给您之前,它可能会经过几层代码,并且在这样的情况下处理 IDisposable 可能并不简单。一个案例。
也许您解析的接口不是继承自IDisposable,但实际使用的服务类继承了? 怎么处理呢? ETC。
Also, depending on whether you use systems like that, if you're using an IoC container, it might go through several layers of code before the service is returned to you through an interface, and it might not be trivial to handle IDisposable in such a case.
Perhaps the interface you resolved doesn't inherit from IDisposable, but the actual service class being used does? How to handle that? etc.
FxCop 中的使用规则 CA2213 (DisposableFieldsShouldBeDispose) 和 CA2215 (DisposeMethodsShouldCallBaseClassDispose) 将捕获在您自己的类中未正确调用 dispose 的位置,但我不相信有任何东西可以检查 dispose 是否始终被调用,尽管讽刺的是有一条规则( CA2202) 用于 DoNotDisposeObjectsMultipleTimes
Usage Rules CA2213 (DisposableFieldsShouldBeDisposed) and CA2215 (DisposeMethodsShouldCallBaseClassDispose) within FxCop will catch where dispose isn't called correctly in your own classes but i don't believe there is anything out there to check dispose is always called though ironically there is a rule (CA2202) for DoNotDisposeObjectsMultipleTimes
您可以使用 ReSharper 来完成此操作。 借助 ReSharper,您可以使用 Alt-End 轻松导航任何接口的实现,但对于诸如
IDisposable
之类的流行接口来说,这是不切实际的。您可以执行以下操作:
System.IDisposable
IDisposable
的(解决方案的)所有类型的列表。 那些粗体是您想要的 - 它们直接实现IDisposable
。希望有帮助。
You could do this with ReSharper. With ReSharper you can navigate implementations of any interface with ease by using Alt-End, but for a popular interface such as
IDisposable
this is not practical.Here's what you could do:
System.IDisposable
IDisposable
. Those in bold are the ones you want - they implementIDisposable
directly.Hope that helps.
我明白你的意思。 我不知道,但看看 FxCop。 它可能在某处有一个规则来检查实现 IDisposable 的对象是否未释放。 只是一种预感,请注意。
更新:米奇·小麦写道:
谢谢,Mitch。
I know what you mean. I don't know, but look at FxCop. It might have a rule in there somewhere that checks whether objects implementing IDisposable are not disposed. Just a hunch, mind.
UPDATE: Mitch Wheat writes:
Thanks, Mitch.