Skip to content

Commit

Permalink
Free SDL surface after updating the window icon
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thefiddler committed Sep 29, 2013
1 parent 420ec23 commit cfdb5bc
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions Source/OpenTK/Platform/SDL2/Sdl2NativeWindow.cs
Expand Up @@ -407,24 +407,37 @@ public Icon Icon
}
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);
}

}
else
{
// Clear the window icon
SDL.SDL_SetWindowIcon(window.Handle, IntPtr.Zero);
}
SDL.SDL_SetWindowIcon(window.Handle, surface);

icon = value;
IconChanged(this, EventArgs.Empty);
}
Expand Down

0 comments on commit cfdb5bc

Please sign in to comment.