Class ComponentsContainer ' a component contains other components'
Inherits System.ComponentModel.Component
Private foo as New Component
Private bar as New Component
Protected Override Sub Finalize()
foo.Dispose() ' HERE ? '
bar.Dispose()
MyBase.Finalize()
End Sub
Protected Overrides Sub Dispose(disposing As Boolean)
If disposing Then
foo.Dispose() ' OR HERE ? '
bar.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
End Class
You should not (don't have to) Dispose managed resources from the Finalizer:
Protected Override Sub Finalize()
' foo.Dispose() '
' bar.Dispose() '
MyBase.Finalize()
End Sub
And from that it follows that if your class does not have unmanaged resources you don't need a Finalizer at all.
Note: Your class is missing the Public Sub Dispose()
overload.
Edit:
Since foo
and bar
are managed resources (extending Component) you only need the Protected Overrides Sub Dispose(disposing As Boolean)
method. The version in the question is correct. And simply drop the Finalize()
.