Re: Finalizer vs. Dispose

From: Brock Allen (ballen_at_develop.com.i_hate_spam_too)
Date: 03/18/05


Date: Thu, 17 Mar 2005 16:39:54 -0800


> Mostly all .NET classes which implements IDisposable, also implements
> an finalizer.

I disagree. The Finalizer is really there for non-managed resources that
the GC has no idea how to properly cleanp. Think Win32 handles that your
code went out thru interop to acquire. If you don't have those sorts of resources,
then don't implement a Finalizer as it slows your app down and the memory
for your objects hang around longer than they need to. I'd say the converse
is more accurate: "All classes that have a Finalizer should also implement
IDisposable".

IDisposable, as already mentioned, is just a pattern that alerts the client
of your code that Dispose needs to be called as soon as you're done with
this object. This allows more aggresive cleanup of resources (both managed
and unmanaged). If all you hold are managed resources, then IDisposable is
sufficient, yet important.

-Brock
DevelopMentor
http://staff.develop.com/ballen



Relevant Pages

  • Re: " //Clean Up managed resources " f&*ck
    ... The finalizer is called by the GC. ... time (such as releasing internal unmanaged resources). ... Since Dispose ... it is about 'managed resource'. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Dipose vs Destructor
    ... resources and native resources in a managed C++ class? ... C++/CLI implements the destructor and finalizer semantics in any ref class T ... These cleanup mechanisms are hidden from the C++/CLI ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Finalizer vs. Dispose
    ... You can read more on Dispose guidelines here: ... |> Mostly all .NET classes which implements IDisposable, ... The Finalizer is really there for non-managed resources that ...
    (microsoft.public.dotnet.framework.clr)
  • Re: GC and dispose
    ... resources as well as unmanaged. ... We tend to use the Finalizer ... The Dispose method should be used to free resources. ... protected virtual void Dispose ...
    (microsoft.public.dotnet.framework)
  • Re: Finalizer vs. Dispose
    ... I think when we have the IDisposable interface ... > finalizer is better than implementing the IDisposable interface? ... Implement IDisposable if you need to free unmanaged resources. ... Mostly all .NET classes which implements IDisposable, ...
    (microsoft.public.dotnet.framework)