Implemented IDisposable in Sdl2Joystick

Sdl2Joystick calls SDL_JoystickOpen, which means we must call
SDL_JoystickClose on shutdown.
This commit is contained in:
Stefanos A 2013-10-04 10:00:57 +02:00
parent 79def664b8
commit bad2b7b6d7

View file

@ -27,11 +27,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using OpenTK.Input; using OpenTK.Input;
namespace OpenTK.Platform.SDL2 namespace OpenTK.Platform.SDL2
{ {
class Sdl2JoystickDriver : IJoystickDriver, IGamePadDriver class Sdl2JoystickDriver : IJoystickDriver, IGamePadDriver, IDisposable
{ {
struct Sdl2JoystickDetails struct Sdl2JoystickDetails
{ {
@ -43,6 +44,7 @@ namespace OpenTK.Platform.SDL2
readonly List<JoystickDevice> joysticks = new List<JoystickDevice>(); readonly List<JoystickDevice> joysticks = new List<JoystickDevice>();
IList<JoystickDevice> joysticks_readonly; IList<JoystickDevice> joysticks_readonly;
bool disposed = false;
public Sdl2JoystickDriver() public Sdl2JoystickDriver()
{ {
@ -83,7 +85,6 @@ namespace OpenTK.Platform.SDL2
joysticks.Add(joystick); joysticks.Add(joystick);
} }
} }
} }
#endregion #endregion
@ -145,6 +146,46 @@ namespace OpenTK.Platform.SDL2
} }
#endregion #endregion
#region IDisposable Members
void Dispose(bool manual)
{
if (!disposed)
{
if (manual)
{
Debug.Print("Disposing {0}", GetType());
foreach (var j in joysticks)
{
var joystick = (JoystickDevice<Sdl2JoystickDetails>)j;
IntPtr handle = joystick.Details.Handle;
SDL.SDL_JoystickClose(handle);
}
joysticks.Clear();
}
else
{
Debug.Print("{0} leaked, did you forget to call Dispose()?", GetType());
}
disposed = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~Sdl2JoystickDriver()
{
Dispose(false);
}
#endregion
} }
} }