When using won't Dispose
using statement/block in C# (not the one used to pull in namespaces) is meant to aid in the IDisposable pattern, i.e. cleaning up resources that won't be handled by garbage collection and to do so in a deterministic fashion. I.e. everything that finalization is not. It really is just syntactic sugar to avoid
try/finally all the time. I.e. this
is pretty much the same as
But there is a common pitfall with using, err,
using. It's in the first line above: The disposable object is created outside the
try/finally block! Now, a constructor failing shouldn't ever have allocated disposable resources, so you're usually safe here. But beware if the construction of the Disposable object is a Method.
CreateDisposable() fails after it has created
Disposable, you'll end up with a resource leak!
You can easily avoid this by catching failure in your method and cleaning up, but you can't use
using for this purpose, since success would return an already disposed instance. A safe implementation of
CreateDisposable() looks like this:
IDisposable is an important pattern in .NET, but because it is a pattern rather than a construct enforced by the compiler, it is a common source of "leaks" in .NET.
using is very useful for handling the disposable, but it is important to remember that the only code covered by the automatic disposition logic is the code inside the
using block, not the code inside the