Re: GC and dispose



"Göran Andersson" <guffa@xxxxxxxxx> wrote in message
news:evglUFYlGHA.2128@xxxxxxxxxxxxxxxxxxxxxxx
Markus Stoeger wrote:
Simon Hart wrote:
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.

Could you explain why you prefer the finalizer instead of a manual call
to Dispose for freeing unmanaged resources?

As far as I know you can never tell when the GC will finalize an object.
So it might keep floating around forever as well. Which means that you
could run out of handles for example.

Maybe I've misunderstood something behind GC and the finalizer?.

Max

No, that is correct.

The Dispose method should be used to free resources. The finalizer should
call Dispose as a backup if the program failed to call it.

More specifically, the recommended structure for Finalize and Dispose is:

class C : IDisposable
{
void IDisposable.Dispose()
{
Dispose(true);
}

~C()
{
Dispose(false)
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// free managed resources here

GC.SuppressFinalize(this);
}

// free unmanaged resources here
}
}

The C++/CLI compiler automatically implements this structure, while in C#/VB
you have to do it yourself. Current versions of FxCop will raise warnings
if your class implements IDisposable but doesn't do it this way.

Of course, if your class cannot be derived from, then there's no need for
Dispose(bool) to be virtual.

-cd

-cd


.



Relevant Pages

  • 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: Question on implementing IDisposable
    ... have dealth with the unmanaged resources and before you take your ... This class does not implement a finalizer ... The overloaded Dispose which takes a bool argument should be made ... Then, the base class can implement IDispose, so ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How much do you leave to VB.Net GARBAGE COLLECTION???
    ... as necessary due to the Garbage Collection capability. ... those that implement IDisposable and have a Dispose method. ... From the Finalizer, ... asynchronously and unmanaged resources might not be freed in a timely ...
    (microsoft.public.dotnet.languages.vb)
  • Re: " //Clean Up managed resources " f&*ck
    ... The finalizer is called by the GC. ... time (such as releasing internal unmanaged resources). ... Since Dispose ... managed code is computer ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How much do you leave to VB.Net GARBAGE COLLECTION???
    ... as necessary due to the Garbage Collection capability. ... those that implement IDisposable and have a Dispose method. ... From the Finalizer, ... asynchronously and unmanaged resources might not be freed in a timely ...
    (microsoft.public.dotnet.languages.vb)