开发者

Catching IDisposables that are never disposed

So, as a new .NET programmer I thought that the garbage collector would clean up my mess for me all the time.

Now, a y开发者_JAVA百科ear and a half later I am getting out of memory exceptions in that code I wrote when I did not know that I had to call dispose on some resources. I am guessing that is because that code allocats a lot of bitmaps that don't get disposed...

I have gone through the code to clean these up now that I know better. But I keep missing some. Is there a tool or setting that can see IDisposables not getting disposed?

While I can see the simple cases being easy to catch by compiler or tool, I can also see some more complex scenarios being very hard to catch. So if there is no tool I understand and will keep doing it by hand.


Take a look at FxCop. One of its rules will catch items that implement IDisposable, where Dispose() wasn't called.


A memory profiler will be able to show you where all live objects of a given class where allocated from - this can be a good starting point.

Some of the memory profiler now claim to track missing calls to Dispose(), - I don't know how well they work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜