I'm having a little trouble understanding the IDisposable interface
I'm developing a Game Engine and have ran code analysis on my solution and been told to implement the IDisposable interface on my "GrapicsEngine" class, because it contains a Bitmap instance.
Naturally, I searched the internet to learn how to do this correctly and have came up with the following code:
Here is my class members:
private const int BITMAP_WIDTH = 4096;
private const int BITMAP_HEIGHT = 4096;
private Graphics backBuffer;
private Bitmap bitmap;
private Color clearColor;
private WindowSurface surface;
private GraphicsQuality quality;
private Viewport viewport;
and here is my IDispoable region:
#region IDisposable
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing == true)
ReleaseManagedResources();
ReleaseUnmanagedResources();
}
private void ReleaseManagedResources()
{
if (bitmap != null)
bitmap.Dispose();
if (backBuffer != null)
backBuffer.Dispose();
}
private void ReleaseUnmanagedResources()
{
}
~GraphicsEngine()
{
Dispose(false);
}
#endregion
Note that WindowSurface is a WinForm Panel, GraphicsQuality is an enum and Viewport contains only int values.
So I have just a couple questions:
- Am I disposing of my resources properly?
- What resources should I dispose of in the Unmanaged resources method
- If I create a new class and it contains my GraphicsEngine class, should that class also implement IDisposable?
Any help on this topic would be greatly appreciated.