From bad2b7b6d71a464ab2622f3779d1fa5ad1239b17 Mon Sep 17 00:00:00 2001 From: Stefanos A Date: Fri, 4 Oct 2013 10:00:57 +0200 Subject: [PATCH] Implemented IDisposable in Sdl2Joystick Sdl2Joystick calls SDL_JoystickOpen, which means we must call SDL_JoystickClose on shutdown. --- .../Platform/SDL2/Sdl2JoystickDriver.cs | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/Source/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs b/Source/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs index 7c05ff00..a0741d11 100644 --- a/Source/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs +++ b/Source/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs @@ -27,11 +27,12 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using OpenTK.Input; namespace OpenTK.Platform.SDL2 { - class Sdl2JoystickDriver : IJoystickDriver, IGamePadDriver + class Sdl2JoystickDriver : IJoystickDriver, IGamePadDriver, IDisposable { struct Sdl2JoystickDetails { @@ -43,6 +44,7 @@ namespace OpenTK.Platform.SDL2 readonly List joysticks = new List(); IList joysticks_readonly; + bool disposed = false; public Sdl2JoystickDriver() { @@ -83,7 +85,6 @@ namespace OpenTK.Platform.SDL2 joysticks.Add(joystick); } } - } #endregion @@ -145,6 +146,46 @@ namespace OpenTK.Platform.SDL2 } #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)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 } }