Tutorial :Tracking undisposed Disposable objects



Question:

Is there a tool that can scan your code and determine which objects that implement IDisposable are not being disposed in a code base at compile time or runtime?

I have possible areas in the code that are not disposing objects but it's hard to look back and see which objects require this in the first place.


Solution:1

There is a lot of static analysis tooling, which can help here.

Both CodeRush/Refactor Pro and Resharper will, at code time, in Visual Studio, show you undisposed objects.

And FxCop, now packaged as part of Visual Studio Code Analysis can generate compile time warnings for undisposed locals and class members.


Solution:2

The ANTS memory profiler from red-gate will help with this at runtime. It's one of my favorite tools.


Solution:3

The destructor of a class is called when the garbage collector finally cleans up classes that are no longer in use. What you could do is put a check in the destructor that "Dispose" has been called. It is advised against actually using the destructor, but for testing purposes, it can be useful.

The only problem with this method is that you will have to create your own class that inherits from the one you need to test for IDispose and override the Dispose method (so you can set a flag saying that it has been called), initialize the flag in a constructor and check it in the destructor (which you implement with "~ClassName() { ... }")


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »