Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

c# - Detecting "leaked" IDisposable objects

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...