Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overrides Sub Finalize()
Dispose(False)
End Sub
End Class
Let??™s analyze the changes made to support a finalizer. You add a Finalize method and a
second level of indirection in the Dispose implementation. This is so you know if your private
Dispose method got called from a call to Dispose() or through the finalizer. Also, in this example,
you implement the Dispose(Boolean) method as Overridable, so that any deriving type
merely has to override this method to modify the dispose behavior. If the Win32Heap class were
marked NotInheritable, you could change that method from Protected to Private and remove
the Overridable keyword. Remember, you cannot reliably use subobjects if your Dispose method
got called from the finalizer.
nNote Some people take the approach that all object references are off limits inside the Dispose method
that is called by the finalizer. There??™s no reason you cannot use objects that you know to be alive and well.
However, beware if the finalizer is called as a result of the application domain shutting down; objects that
you assume to be alive may not actually be alive. In reality, it??™s almost impossible to determine if an object
reference is still valid in 100% of the cases, and it??™s probably best to not reference any reference types
within the finalization stage if you can avoid it.
Pages:
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552