Game Development Reference
In-Depth Information
protected virtual void Dispose(bool disposing)
{
if (!this.m_IsDisposed)
{
if (disposing)
{
// Unregister eventsm_Form.FormClosed -=
this.FormClosed;
// get rid of managed resources here
}
// get rid of unmanaged resources here
}
m_IsDisposed = true;
}
The
Dispose(bool)
method is protected because we do not want it to be called
from outside of the
GameWindow
class. Instead, we will create the second
Dis-
pose()
method that is
public
and has no parameters. This way we can tell the
object that we are done with it by calling this public method, and it will take care of
cleaning up after itself. The
Dispose(bool)
method starts by checking to see if
the game window has already been disposed of. If not, then it checks to see if the
disposing
parameter is set to
true
. This parameter simply indicates whether the
game window is disposing of itself or whether it is being disposed of by the
Garbage
Collector
.
The Garbage Collector is a part of managed languages such as C#. When your pro-
gram is no longer using an object, the Garbage Collector will reclaim the memory
that object was using so that it can be used for something else. This will only happen
if the program no longer has any references to the object. However, the Garbage
Collector is not guaranteed to reclaim that memory immediately.
Tip
You should avoid creating large numbers of objects that you only use for a short
time. This can keep the Garbage Collector much busier and you will pay the price
in the form of lower FPS or lag spikes.