Tech Off Thread

2 posts

To Dispose or Not To Dispose

Back to Forum: Tech Off
  • User profile image
    endymion853

    I am trying to figure out best practices for implementing the Dispose method in business objects that I develop. 

    Some sources I have seen says you should always have a Dispose method on all of your classes so that a) you get used to calling them and b) so you remember to check that you are cleaning up after yourself.

    However, what happens if I am not using any managed resources that I need to clean up after?  Is there a performance penalty implementing IDisposable and not having the Dispose method do anything?

    Also, what happens if you have developes who incorrectly implement the dispose method?  Do the resources ever get removed then?  Let's say someone is using a global file handle and then forgets to close it in the dispose method.  What are the implications?

    Any experiences out there?  What are other people doing?

    Thanks
    Endymion

  • User profile image
    spod

    Hi endymion853

    I'd say you only want to implement the dispose pattern when you need to. Typically you wouldn't for business objects in my experience, as most of the resources you are wrapping ( database connection handles etc ) are scoped to individual method calls.

    If you are wrapping unmanaged / expensive shared resources then you will definitely want to implement dispose for these, and make sure they are always explicitly disposed ( enforce the using() pattern in c# maybe? )


    endymion853 wrote:

    However, what happens if I am not using any managed resources that I need to clean up after?  Is there a performance penalty implementing IDisposable and not having the Dispose method do anything?


    I would definitely advise NOT doing this. There's a few reasons:


    You remove a lot of the benefits of having a garbage collector and .net in general ( you have to worry about matching allocates with deletes again ).

    The pattern is a little subtle, and easy to get wrong( say fogetting to call Gc.SupressFinalize(this)). You want to avoid finalizers being called on a server app if possible as there's a definite cost to them, even if they do nothing.

    .net is tuned towards a garbage collected environment. If you make all your objects implement dispose you change the memory patterns of your application which ( may ) make things worse ( not sure here, i've never measured it ).

    endymion853 wrote:

    Also, what happens if you have developes who incorrectly implement the dispose method?  Do the resources ever get removed then?  Let's say someone is using a global file handle and then forgets to close it in the dispose method.  What are the implications?


    It depends how badly they mis-implement them Smiley

    If the handle is one that you got through a framework class( say File.Open() ) then the file handle will be closed eventually. The problem is that this is non-deterministic, so it basically happens when the garbage collector needs to collect. You won't leak hnadles etc in this case, but if another thread tries to open the same global file with exclusive access at this time it would throw presumably.

    If you've written interop code and have called CreateFile() or somesuch win32 api directly then you will also leak this handle if you don't correctly close it in the finalizer / implement dispose etc.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.