Game Development Reference
In-Depth Information
If the
disposing
parameter is set to
true
, then the code inside the
if
statement
will unregister events and call the
Dispose()
method on any managed objects it is
using. Currently, it only unregisters the
Closed
event that we previously subscribed
to. Then, the code following the
if
statement cleans up any unmanaged objects that
it may be using. We won't have any unmanaged objects since we are using only
managed
code. Managed means that memory management is handled automatic-
ally for us for the most part.
Lastly, at the end of this method, the
m_IsDisposed
member variable is set to
true
. This indicates that the window has been disposed of so the
RenderScene()
method will know that it should not try to render anything anymore, as doing so can
cause the program to crash. We'll discuss the
RenderScene()
method in a mo-
ment, but first we need to finish with
IDisposable
.
Now we have to implement the public
Dispose()
method, which is a very short
method as you can see in the following code snippet:
public void Dispose()
{
Dispose(true);
// Since this Dispose() method already
cleaned up the resources
used
// by this object, there's no need for the
Garbage Collector to
call
// this class's Finalizer, so we tell it not
to
GC.SuppressFinalize(this);
}
This method calls the
Dispose(bool)
method to dispose of the game window.
The value
true
is passed in because this method is part of the
GameWindow
class,
and thus the
GameWindow
class is disposing of itself in this case. Then we call
GC.SuppressFinalize(this)
to tell the Garbage Collector that this object has
already been disposed of. You may have noticed that we never implemented
Fin-