From 744fbdd26fb8a84788154158a48a54b46278caa2 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Fri, 30 Jun 2006 19:08:20 +0000 Subject: [PATCH] Windows: Fixed Display.setIcon memory leak --- src/native/win32/org_lwjgl_opengl_Display.c | 28 ++++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/native/win32/org_lwjgl_opengl_Display.c b/src/native/win32/org_lwjgl_opengl_Display.c index fc09db0d..2d3610ea 100644 --- a/src/native/win32/org_lwjgl_opengl_Display.c +++ b/src/native/win32/org_lwjgl_opengl_Display.c @@ -49,6 +49,8 @@ #include "org_lwjgl_opengl_Win32Display.h" #include "context.h" +static HICON small_icon = NULL; +static HICON large_icon = NULL; static HWND display_hwnd = NULL; // Handle to the window static HDC display_hdc = NULL; // Device context static bool isFullScreen = false; // Whether we're fullscreen or not @@ -110,6 +112,20 @@ static void appActivate(bool active) inAppActivate = false; } +static void freeLargeIcon() { + if (large_icon != NULL) { + DestroyIcon(large_icon); + large_icon = NULL; + } +} + +static void freeSmallIcon() { + if (small_icon != NULL) { + DestroyIcon(small_icon); + small_icon = NULL; + } +} + JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Win32Display_didMaximize (JNIEnv *env, jobject self) { jboolean result = did_maximize ? JNI_TRUE : JNI_FALSE; @@ -278,6 +294,8 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Win32Display_destroyWindow(JNIEnv * closeWindow(&display_hwnd, &display_hdc); if (isFullScreen) ClipCursor(NULL); + freeLargeIcon(); + freeSmallIcon(); } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Win32Display_switchDisplayMode(JNIEnv *env, jobject self, jobject mode) { @@ -461,10 +479,11 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_Win32Display_nSetWindowIcon16 { int *imgData = (int *)(*env)->GetDirectBufferAddress(env, iconBuffer); - HICON newIcon = createWindowIcon(env, imgData, 16, 16); + freeSmallIcon(); + small_icon = createWindowIcon(env, imgData, 16, 16); if (newIcon != NULL) { if (display_hwnd != NULL) { - SendMessage(display_hwnd, WM_SETICON, ICON_SMALL, (LPARAM) (newIcon)); + SendMessage(display_hwnd, WM_SETICON, ICON_SMALL, (LPARAM) (small_icon)); return 0; } @@ -478,10 +497,11 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_Win32Display_nSetWindowIcon32 { int *imgData = (int *)(*env)->GetDirectBufferAddress(env, iconBuffer); - HICON newIcon = createWindowIcon(env, imgData, 32, 32); + freeLargeIcon(); + large_icon = createWindowIcon(env, imgData, 32, 32); if (newIcon != NULL) { if (display_hwnd != NULL) { - SendMessage(display_hwnd, WM_SETICON, ICON_BIG, (LPARAM) (newIcon)); + SendMessage(display_hwnd, WM_SETICON, ICON_BIG, (LPARAM) (large_icon)); return 0; }