Can Visual Studio 2008 be configured to give me a warning when I forget to dispose an object that implements IDisposable?
views:
255answers:
4I don't think it can be done at compile time.
however, it can be done at runtime.
I have created an abstract class 'Disposable' (which implements IDisposable, and implements the Disposable pattern).
In the finalizer, I issue an Assert when the finalizer is called, and the object has not been disposed.
I've based this on an article of Ian Griffiths:
http://www.interact-sw.co.uk/iangblog/2004/04/26/yetmoretimedlocking
I'm not sure if you're using C# or VB, but in C#, the "best practice" way to handle objects of type IDisposable is to place the code in a using block.
"The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources." - MSDN Link
HTH,
-Dan
Visual Studio, by itself does not have this feature, but with CodeRush you can have design time warnings and refactorings to insert using blocks where needed.
If you turn on FxCop Design rules it will tell you when you don't implement IDisposable and you have members which implement IDisposable, like this:
class Program
{
private DataTable NotDisposed;
public Program()
{
NotDisposed = new DataTable();
}
static void Main()
{
}
}