For example, you should
create a finalizer when your object manages some sort of unmanaged resource. Finally, any
object that has a finalizer should implement the Disposable pattern, which the upcoming
section titled ???Disposable Objects??? covers.
Exception Handling
In VB, the runtime will treat an exception thrown in a finalizer that leaves the block uncaught
as an unhandled exception, and by default, the process will terminate after notifying you of
the exception.
Disposable Objects
Any object that has a finalizer must implement the IDisposable interface. IDisposable is not a
perfect replacement for any type of deterministic finalization, but it does get the job done at
the expense of adding complexity to the client of your objects.
The IDisposable Interface
The IDisposable definition is as follows:
Public Interface IDisposable
Sub Dispose()
End Interface
Notice that it has only one method, Dispose(), and it is within this method??™s implementation
that the dirty work is done. Thus you should completely clean up your object and release all
resources inside Dispose(). Even though the client code rather than the system calls Dispose()
automatically, it??™s the client code??™s way of saying, ???I??™m done with this object and don??™t intend to
use it ever again.
Pages:
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133