From cfdb5bc95afa376ed16e7052cc8c416e5697588f Mon Sep 17 00:00:00 2001 From: "Stefanos A." Date: Sun, 29 Sep 2013 23:36:28 +0200 Subject: [PATCH] Free SDL surface after updating the window icon The SDL surface is no longer needed and will cause a memory leak if not freed. Additionally, the decoded BitmapData should not be unlocked until after updating the window icon, as it may be moved by the GC otherwise. --- .../OpenTK/Platform/SDL2/Sdl2NativeWindow.cs | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs b/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs index 75d65a71..bcd5dd18 100644 --- a/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs +++ b/Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs @@ -407,24 +407,37 @@ namespace OpenTK.Platform.SDL2 } set { - IntPtr surface = IntPtr.Zero; + // Set the new icon, if any, or clear the current + // icon if null if (value != null) { using (Bitmap bmp = value.ToBitmap()) { + // Decode the icon into a SDL surface var data = bmp.LockBits( new Rectangle(0, 0, value.Width, value.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); - surface = SDL.SDL_CreateRGBSurfaceFrom( - data.Scan0, data.Width, data.Height, 32, data.Width * 4, + IntPtr surface = SDL.SDL_CreateRGBSurfaceFrom( + data.Scan0, data.Width, data.Height, 32, data.Stride, 0x00ff0000u, 0x0000ff00u, 0x000000ffu, 0xff000000u); + // Update the window icon + SDL.SDL_SetWindowIcon(window.Handle, surface); + + // Free the SDL surface as it is no longer needed + SDL.SDL_FreeSurface(surface); bmp.UnlockBits(data); } + } - SDL.SDL_SetWindowIcon(window.Handle, surface); + else + { + // Clear the window icon + SDL.SDL_SetWindowIcon(window.Handle, IntPtr.Zero); + } + icon = value; IconChanged(this, EventArgs.Empty); }