Re: GC and dispose



I don't know about the C# environment, but VB 2005 can be configured to
insert the dispose pattern, as well as other implementation patterns, when
you add the Implements clause to the class definition and press return.

Mike Ober.

"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@xxxxxxxxxxxxxxx>
wrote in message news:%23V$RCfYlGHA.1240@xxxxxxxxxxxxxxxxxxxxxxx
"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: Need explanation on calling Close() from finalizer.
    ... understand is why .NET classes cannot clean up their unmanaged resources ... > NET's Garbage Collector, like you've stated, automated memory management, ... > the Dispose pattern. ... >>> hoping your finalizer will someday be called when the garbage collector ...
    (microsoft.public.dotnet.framework.adonet)
  • 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: Clean Up managed resources
    ... these have unmanaged resources. ... If somebody called Dispose on your object, ... If the finalizer is called on your object, ... threads while it determines reachability from all the different roots. ...
    (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: Disposing of brushes
    ... fonts etc take GDI handles - and you could run short of those ... unmanaged resources, the number one reason to have IDisposable? ... in this special case the finalizer will call Disposefor you, ... Dispose() in the finalizer and supress the finalizer if Dispose is ...
    (microsoft.public.dotnet.languages.csharp)