检测"泄露" IDisposable的对象 [英] Detecting "leaked" IDisposable objects

查看:105
本文介绍了检测"泄露" IDisposable的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多问题,这样问如何检测对象的IDisposable泄漏。这似乎是答案是你不能



我只是检查用最平凡的测试情况下,FxCop的10.0不这样做,ReSharper的4 MSVS2010没有做到这一点。


$ b $ :b

这似乎是我错了,不如在C(对于其中至少我们已经建立工具来检测)内存泄漏



我在想:是不是可能的话,使用反射和其他模糊的先进技术,我可以注入在运行时的检查,在终结,看是否的Dispose 被称为?



如何变魔术使用WinDbg + SOS?



即使没有现有的工具来做到这一点,我想知道这是否在理论上是可能的(我的C#是不是很锐利)。



想法?



注:这个问题的标题可能是误导性的。这里真正的问题应该是的的IDisposable 对象是否已处置()正确的。 。入门布置在由GC不算数,因为我认为这是一个错误。



修改的:解决方法:.NET内存分析器做工作。我们只需要几个垃圾邮件 GC.Collect的()在节目的最后,使我们的探查正确地拿起统计信息。


< DIV CLASS =h2_lin>解决方案

您没有搜索够硬。有大量的.NET内存分析器在那里会看你的节目,因为它运行,并让你知道在哪里/如何你的内存使用(什么是漏水的话)。



我想看看以下任何一项:



Microsoft's CLR内存分析器(免费)

展鹏蚂蚁内存分析器

JetBrain的DotTrace(包括代码分析器以及)

赛特.NET内存分析器



更新



赛特的.NET内存分析器有一个功能叫做废弃跟踪适合只跟踪的Dispose在自己的应用程序调用的OP的请求的议案。


There are many questions SO asking how to detect IDisposable objects leak. It seems like the answer is "you can't".

I just checked with the most trivial test case, that FxCop 10.0 doesn't do it, ReSharper 4 with MSVS2010 doesn't do it.

This seems wrong to me, worse than memory leaks in C (for which at least we have established tools to detect).

I was thinking: Is it possible, using reflection and other obscure advanced techniques, that I can inject a check at runtime, in the finalizer to see whether Dispose has been called?

How about magic tricks with WinDBG+SOS?

Even if there aren't existing tools to do it, I'd like to know whether this is theoretically possible (my C# isn't very sharp).

Ideas?

NOTE This question's title might have been misleading. The real question here should be whether a IDisposable object has been Disposed() properly. Getting disposed by the GC doesn't count since I regard that as a mistake.

Edit: Solution: .NET Memory Profiler does the job. We just need to spam several GC.Collect() at the end of the program to enable our profiler to correctly pick up the stats.

解决方案

You didn't search hard enough. There are plenty of .NET Memory Profilers out there that will look at your program as it runs and let you know where/how your memory is used (and what is leaking it).

I would check out any of the following:

Microsoft's CLR Memory Profiler (free)
RedGate ANTS Memory Profiler
JetBrain's DotTrace (includes code profiler as well)
SciTech .NET Memory Profiler

Update

SciTech's .NET Memory Profiler has a feature called 'Dispose Tracker' that fits the bill for the OP's request of tracking only the Dispose calls in their application.

这篇关于检测&QUOT;泄露&QUOT; IDisposable的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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