AJ Warnock

This Page Left intentionally Blank
posts - 36, comments - 7, trackbacks - 8

My Links

News




Archives

Post Categories

Developer Blogs

Development Community

Its a Disposable World...

While this is pretty old hat stuff, it appears that the statement “common sense is not always common practice” continues to hold true.

 

Recently while researching some code, I found several instances where someone neglected to call the Dispose() method on a SqlCommand object.  It appears that they believe that calling the Dispose() method on the connection actually called the method on the associated command.  Some developers believe that the disposing of the associated connection will dispose of the associated command.  Neither of these are the case.

 

In the beginning, there were many Microsoft Examples that did not call the SqlCommand objects Dispose() method due to the fact that it did not override this method and only called the base method.  Based on this knowledge, it was not really necessary to call this method.   The problem occurs when we follow this practice for multiple objects and suddenly the method is overridden in a new release of the framework.  Just by following this seemingly harmless and potentially logical technique, we have inadvertently introduced latent memory and resource leaks into the code.

 

The best technique for ensuring that this does not occur is to appropriately use the using keyword or ensure that the dispose() method is called in the finally blocks of any try-catch-finally exception processing  to ensure that we always dispose of any object that is disposable.

 

A hard and fast rule of “If it implements IDisposable, then always call Dispose()” will always keep your code unambiguous and dependable regardless of any modifications in the framework.

Print | posted on Tuesday, February 14, 2006 8:55 AM | Filed Under [ C# .Net ]

Feedback

Gravatar

# re: Its a Disposable World...

If a SqlCommand object (100% managed) requires a Dispose to work properly, then something is wrong. We all should be getting away from Deterministic disposal as much as possible.

Now dealing with SqlCommand "suddenly implementing IDisposable", that could apply to ANY component... should we just start disposing EVERYTHING, just in case? By your logic we should. Truth is, as frameworks evolve the code that depends on it should evolve too OR stick with the CURRENT framework it was written for. (DLL hell anyone?)
2/14/2006 1:37 PM | Eric Newton
Gravatar

# re: Its a Disposable World...

Sorry, it appears I have hit a nerve!

Yes! We should get away from deterministic disposal as a practice; however, currently we must live with it. I did not intend to imply that everyone should create and call a Dispose() method for each object. I merely intended to say we should support and use the appropriate interfaces when it was provided

By not calling the method as prescribed and expected by the creator and owner of the object, we prevent the extension of the object without changing the interface. Besides that you are invading into the “private” territory of an object that should remain “private”. Because, we can discern what is going on inside an objects implementation does not give us license to code to undocumented features and current internal functionality. And, while some do chose to tolerate and employ this practice, it is extremely risky to say the least.

I believe that as the creator of an object I should wisely and cautiously architect our public interfaces to ensure that we do not include useless methods that provide no real functionality and serve only to clutter the namespace and interface definition.

In this case, the object was derived from IDisposable theoretically for a reason. Just because it does not currently overload the method should not be reason enough to not follow the prescribed usage.

You are definitely correct interfaces are contracts and we TRUST that they will not change. However, TRUST is bidirectional and requires two contacts. As a provider of an interface, I too have to TRUST that my interface will be used somewhat appropriately and as I prescribe so that I can ensure that it functions to specifications.
2/15/2006 7:17 AM | AJWarnock
Comments have been closed on this topic.

Powered by: