Re: GC and dispose



Agree. We often use the Dispose (IDisposable) for cleaning managed resources
as well as unmanaged. We tend to use the Finalizer (Destructor) for
unmanaged deallocation.

Regards
Simon.

"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@xxxxxxxxxxxxxxx>
wrote in message news:uuTrPmTlGHA.4536@xxxxxxxxxxxxxxxxxxxxxxx
Daniel Billingsley wrote:
As somebody else said, the Dispose pattern is for making sure
UNMANAGED resources inside your classes get cleaned up.

I would take exception with that statement. Dispose is for making sure
resources that are not simply managed heap allocations get cleaned up.
Unmanaged resources such as handles or Win32 memory allocations are fine
examples of such, but there are certainly managed examples as well. A
common example of a managed resource that needs to be explicitly cleaned
up is a callback (delegate) which can keep an object alive long after
you're done with it, consuming both memory and CPU resources making
callbacks to an object that no one cares about anymore.

-cd




.



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)
  • Implementing IDisposible on light objects
    ... neither unmanaged resources nor managed resources which are resource ... if I write an empty Dispose() method: ...
    (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: 2 ways to close a connection
    ... that the fix did not come from calling dispose instead of close. ... > resources used by the object. ... It only closes the connection. ... > Explicitly disposing any object that uses unmanaged resources as soon as you ...
    (microsoft.public.dotnet.languages.vb)