Skip to content
This repository
Browse code

Fixed #4

Sdl2InputDriver.Dispose() would call SDL_DelEventWatch with a different
"user_data" parameter than SDL_AdEventWatch. This caused the EventFilter
to remain registered and subsequently crash when closing and reopening a
window.
  • Loading branch information...
commit 48803bb4d620009ea3b9aab6981f6800a2b3793e 1 parent 255f4e9
thefiddler authored
8 Source/OpenTK/Platform/SDL2/Sdl2.cs
@@ -84,6 +84,10 @@ static string IntPtrToString(IntPtr ptr)
84 84 public static extern void AddEventWatch(EventFilter filter, IntPtr userdata);
85 85
86 86 [SuppressUnmanagedCodeSecurity]
  87 + [DllImport(lib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_AddEventWatch", ExactSpelling = true)]
  88 + public static extern void AddEventWatch(IntPtr filter, IntPtr userdata);
  89 +
  90 + [SuppressUnmanagedCodeSecurity]
87 91 [DllImport(lib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_CreateRGBSurfaceFrom", ExactSpelling = true)]
88 92 public static extern IntPtr CreateRGBSurfaceFrom(IntPtr pixels,
89 93 int width, int height, int depth, int pitch,
@@ -103,6 +107,10 @@ static string IntPtrToString(IntPtr ptr)
103 107 public static extern void DelEventWatch(EventFilter filter, IntPtr userdata);
104 108
105 109 [SuppressUnmanagedCodeSecurity]
  110 + [DllImport(lib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_DelEventWatch", ExactSpelling = true)]
  111 + public static extern void DelEventWatch(IntPtr filter, IntPtr userdata);
  112 +
  113 + [SuppressUnmanagedCodeSecurity]
106 114 [DllImport(lib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_DestroyWindow", ExactSpelling = true)]
107 115 public static extern void DestroyWindow(IntPtr window);
108 116
6 Source/OpenTK/Platform/SDL2/Sdl2InputDriver.cs
@@ -45,7 +45,8 @@ class Sdl2InputDriver : IInputDriver2, IInputDriver
45 45 readonly Sdl2Mouse mouse_driver = new Sdl2Mouse();
46 46 readonly Sdl2JoystickDriver joystick_driver = new Sdl2JoystickDriver();
47 47
48   - readonly EventFilter EventFilterDelegate = FilterInputEvents;
  48 + readonly EventFilter EventFilterDelegate_GCUnsafe = FilterInputEvents;
  49 + readonly IntPtr EventFilterDelegate;
49 50
50 51 static int count;
51 52 bool disposed;
@@ -54,6 +55,7 @@ public Sdl2InputDriver()
54 55 {
55 56 lock (SDL.Sync)
56 57 {
  58 + EventFilterDelegate = Marshal.GetFunctionPointerForDelegate(EventFilterDelegate_GCUnsafe);
57 59 driver_handle = new IntPtr(count++);
58 60 DriverHandles.Add(driver_handle, this);
59 61 SDL.AddEventWatch(EventFilterDelegate, driver_handle);
@@ -188,7 +190,7 @@ void Dispose(bool manual)
188 190 joystick_driver.Dispose();
189 191 lock (SDL.Sync)
190 192 {
191   - SDL.DelEventWatch(EventFilterDelegate, IntPtr.Zero);
  193 + SDL.DelEventWatch(EventFilterDelegate, driver_handle);
192 194 }
193 195 DriverHandles.Remove(driver_handle);
194 196 }
4 Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs
@@ -59,7 +59,8 @@ class Sdl2NativeWindow : INativeWindow, IInputDriver
59 59
60 60 readonly IInputDriver input_driver = new Sdl2InputDriver();
61 61
62   - readonly EventFilter EventFilterDelegate = FilterEvents;
  62 + readonly EventFilter EventFilterDelegate_GCUnsafe = FilterEvents;
  63 + readonly IntPtr EventFilterDelegate;
63 64
64 65 static readonly Dictionary<uint, Sdl2NativeWindow> windows =
65 66 new Dictionary<uint, Sdl2NativeWindow>();
@@ -85,6 +86,7 @@ class Sdl2NativeWindow : INativeWindow, IInputDriver
85 86 IntPtr handle;
86 87 lock (SDL.Sync)
87 88 {
  89 + EventFilterDelegate = Marshal.GetFunctionPointerForDelegate(EventFilterDelegate_GCUnsafe);
88 90 handle = SDL.CreateWindow(title, bounds.Left + x, bounds.Top + y, width, height, flags);
89 91 SDL.AddEventWatch(EventFilterDelegate, handle);
90 92 SDL.PumpEvents();

0 comments on commit 48803bb

Please sign in to comment.
Something went wrong with that request. Please try again.