Dispose and Finalize

  • Thread starter Thread starter celeong
  • Start date Start date
C

celeong

Hi, anybody can help me with this.

I've created a singleton class, and now wants to add destructor to it.

I know we can implement the IDisposable and also overrides the Finalize
method (from the MSDN example). But is it possible that we just implement
the Dispose method, which we will call at the end of the application?

Will there be any memory leak or side effects?

Oh ya, I'm using VB.net.
 
Hi,
I know we can implement the IDisposable and also overrides the Finalize
method (from the MSDN example). But is it possible that we just implement
the Dispose method, which we will call at the end of the application?

Will there be any memory leak or side effects?

You should perform manual Dispose (and implement IDisposable as well) if you
have any unmanaged resources that have to be explicitly freed (or, you
instantiate managed classes that in turn use such resources and should be
disposed). If this is the case, Dispose is the right way.

On the other hand, it is even not guaranteed that Finalize will ever be
called on an instance. Therefore, one should not rely on this method for
freeing any resources.

Memory leaks are possible only if your class or any instantiated classes
allocate memory from an unmanaged heap and then are not properly disposed.
Any memory from the managed heap will be automatically freed by the garbage
collector.
 
You should put your clean-up code in the Finalize method. This method gets
called automatically when the object is garbage collected where you must
remember to call Dispose explicitly.

One note, you should technicaly always implement IDisposable when you add a
finalizer to a class but becuase this class is a Singleton, which is
designed to have the same lifespan as the application, implementing
IDisposable would not add any benefit.
 
How should I dispose of a Singleton class anyway? Any ideas?
Should I set me = nothing ??? or will the gc take care of everything.

This class also will hold a reference to an ActiveX COM object during
runtime.
Because it is an unmanaged object, I should release the reference to it
during finalization.
Or shouldn't I?
 
If you don't need to use a finalizer, then you should not as this also
causes more ( maybe un-needed) work for the GC.
 
When the application terminates a final garbage collection is done and all
remaining objects are destroyed. If the objects have a finalizer it will be
called. If you have an internal reference to a COM object you should clean
it up in the finalizer which can be done using the template code below.

Dim objCOM As New ComComponent
' do work
System.Runtime.InteropServices.Marshal.ReleaseComObject(objCOM)
objCOM = Nothing

----
Rob

celeong said:
How should I dispose of a Singleton class anyway? Any ideas?
Should I set me = nothing ??? or will the gc take care of everything.

This class also will hold a reference to an ActiveX COM object during
runtime.
Because it is an unmanaged object, I should release the reference to it
during finalization.
Or shouldn't I?

--------
celeong
Esteem Innovation


Rob Windsor said:
You should put your clean-up code in the Finalize method. This method gets
called automatically when the object is garbage collected where you must
remember to call Dispose explicitly.

One note, you should technicaly always implement IDisposable when you
add
 
Rob, are you sure about putting clean up code in the finalize method? Since
it is possible that finalize may not run in a timely manner (and for some
apps, not at all [theoretically]), why would you rely on it to clear up
memory recourses? I had always been under the assumption that clean up code
should be placed in the Dispose method because you can predict when dispose
will happen as well as cause it to happen.

If I put clean up code in finalize, I may have objects sitting around much
longer than they need to.

-Scott
 
Scott,

Actually, it is recommended to implement both Dispose and Finalize,
especially if you use unmanaged resources. With this pattern, you have much
more guarantee that the resources will be freed if the calling code haven't
called Dispose.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Scott M. said:
Rob, are you sure about putting clean up code in the finalize method? Since
it is possible that finalize may not run in a timely manner (and for some
apps, not at all [theoretically]), why would you rely on it to clear up
memory recourses? I had always been under the assumption that clean up code
should be placed in the Dispose method because you can predict when dispose
will happen as well as cause it to happen.

If I put clean up code in finalize, I may have objects sitting around much
longer than they need to.

-Scott


Rob Windsor said:
You should put your clean-up code in the Finalize method. This method gets
called automatically when the object is garbage collected where you must
remember to call Dispose explicitly.

One note, you should technicaly always implement IDisposable when you
add
a
finalizer to a class but becuase this class is a Singleton, which is
designed to have the same lifespan as the application, implementing
IDisposable would not add any benefit.
 
Back
Top