Dispose and Finalize

  • Thread starter Thread starter Boni
  • Start date Start date
B

Boni

Dear all,
can somebody explain difference between Dispose and Finalize and when each
of them should be used?
Thank you very much,
Boni
 
Hello Boni,

You have five articles in MSDN beginning with this:
Finalize Methods and Destructors:
http://msdn.microsoft.com/library/?...confinalizemethodscdestructors.asp?frame=true


In resume, it says:
Your class should have a Finalize method if it holds unmanaged resources.
Your class should implement IDisposable if it has a Finalize method or if it holds objects that hold unmanaged or expensive resources themselves.

The recommended general aspect of these methods is the following:

Implements IDisposable

'Leave this method as is:
Public Overloads Sub Dispose()Implements IDisposable.Dispose
Dispose(true)
GC.SuppressFinalize(Me) 'Only if the Finalize method exists.
End Sub

'Modify conveniently this method:
Protected Overloads Overridable Sub Dispose(disposing As Boolean)
If (disposing) Then
' Dispose managed resources (like managed files, managed graphic objects, ...).
End If
' Release unmanaged resources (like Windows handles to files, to windows, ...).
End Sub


'Leave this method as is:
Protected Overrides Sub Finalize()
Dispose(false)
End Sub

You must take into account things like thread safety and multiple calls to Dispose, but in general that's all.

Regards.


"Boni" <oilia@nospam> escribió en el mensaje | Dear all,
| can somebody explain difference between Dispose and Finalize and when each
| of them should be used?
| Thank you very much,
| Boni
 
Curtis,
Does anyone know if this applies to the MSHTML class usage in VB.net which
I believe is accessed a COM interface? If so what all needs to be
finalized and destroyed?
Do you not mixing this up with the AxWebBrowser. Although I have put it as
well in a component. Than the complete Idisposable code is created for you.
The same as on a form. Open the designer created code for that.

I hope this helps,

Cor
 
Back
Top