Re: About Dispose

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Nicholas Paldino [.NET/C# MVP] (mvp_at_spam.guard.caspershouse.com)
Date: 10/13/04


Date: Wed, 13 Oct 2004 13:31:51 -0400

Sam,

    If the object in question implements IDisposable, then you should call
it as soon as you possibly can. The reason for this is that it represents
some resource that should be released right away (wether it is memory, file
handles, database connections, etc, etc).

    If you let them linger around, then its true, a proper implementation of
the IDisposable method will make sure that they *eventually* get cleaned up,
but in order to keep your app (and the rest of the system running)
efficiently, it is in your best interests to call the Dispose method as soon
as you are done with them.

    Hope this helps.

"Sam Sungshik Kong" <ssk@chol.nospam.net> wrote in message
news:uIDjsiUsEHA.2144@TK2MSFTNGP10.phx.gbl...
> Hello!
>
> A disposable object's Dispose() method can be called either explicitly by
> the programmer or implicitly during finalization.
> If you call Dispose, the unmanaged resources are released earlier.
> Thus, if you think the unmanaged resources are important, you call Dispose
> explicitly.
>
> My question is what's the criteria to decide the unmanaged resources are
> important.
>
> I think...
> files are important.
> DB connections are important.
>
> What about window handles?
> What about Graphics?
>
> I want to hear what you think about it.
>
> Thanks.
>
> Sam
>
>



Relevant Pages

  • RE: what are unmanaged resources
    ... the GC will come along and clean up all the memory and resources associated ... in a deterministic fashion and clean up unmanaged resources. ... placed inside the Dispose functions. ... sooner than later (i.e. by waiting for the garbage collector) and you should ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Disposing managed resources
    ... can, if it needs, to get more resources. ... using the Dispose() method of that interface. ... understand the .NET memory model very well, nor does he really seem to ... Almost all System.Drawing hold unmanaged resources ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Disposing managed resources
    ... The only case where disposing managed resources *may* be useful is "acquire ... it may help GC to dispose it and release references early so that GC ... > understand the .NET memory model very well, nor does he really seem to ... Almost all System.Drawing hold unmanaged resources and thus must be disposed as soon as possible. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Is Dipose( ) a contractual obligation, if you provide one?
    ... You're at least saying "it'd be a really good idea for you to call Dispose ... relinquish its resources at an explicit point." ... and the finalizer should take care. ... a UEntity unmanaged object requires a USession unmanaged ...
    (microsoft.public.dotnet.languages.vc)
  • Re: IDisposable
    ... You may have managed resources in your object which down the chain eventually contain unmanaged resources. ... In that case, that class shouldn't dispose the object, and thus there's no need to implement IDisposable ... This allows the garbage collector to inform them when they are about to be released, and gives them a chance to release those unmanaged resources even though the client code failed to call IDisposable.Dispose. ... There's no need to call IDisposable.Disposeon objects in your own class's finalizer, even if your class owns the object. ...
    (microsoft.public.dotnet.languages.csharp)