Re: GC and dispose
- From: "Michael D. Ober" <obermd.@.alum.mit.edu.nospam>
- Date: Thu, 22 Jun 2006 11:51:41 GMT
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 messageobject.
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
shouldSo 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
C#/VBcall 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
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
.
- References:
- GC and dispose
- From: Petros Amiridis
- Re: GC and dispose
- From: Phil Wilson
- Re: GC and dispose
- From: Petros Amiridis
- Re: GC and dispose
- From: Daniel Billingsley
- Re: GC and dispose
- From: Carl Daniel [VC++ MVP]
- Re: GC and dispose
- From: Simon Hart
- Re: GC and dispose
- From: Markus Stoeger
- Re: GC and dispose
- From: Göran Andersson
- Re: GC and dispose
- From: Carl Daniel [VC++ MVP]
- GC and dispose
- Prev by Date: Conversion PPC Project from 2003 to 2005
- Next by Date: Re: GC and dispose
- Previous by thread: Re: GC and dispose
- Next by thread: Re: GC and dispose
- Index(es):
Relevant Pages
|