From 895647a240bcdd2342fabdb091cf8b0d74517e3d Mon Sep 17 00:00:00 2001 From: Elias Daler Date: Wed, 9 May 2018 15:06:53 +0300 Subject: [PATCH] Fixed clipboard paste memory leak in SDL examples. (#1803) --- examples/sdl_opengl2_example/imgui_impl_sdl_gl2.cpp | 9 ++++++++- examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/examples/sdl_opengl2_example/imgui_impl_sdl_gl2.cpp b/examples/sdl_opengl2_example/imgui_impl_sdl_gl2.cpp index cf80170b..2238b30a 100644 --- a/examples/sdl_opengl2_example/imgui_impl_sdl_gl2.cpp +++ b/examples/sdl_opengl2_example/imgui_impl_sdl_gl2.cpp @@ -48,6 +48,7 @@ static Uint64 g_Time = 0; static bool g_MousePressed[3] = { false, false, false }; static GLuint g_FontTexture = 0; static SDL_Cursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = { 0 }; +static char* g_ClipboardTextData = NULL; // OpenGL2 Render function. // (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop) @@ -135,7 +136,10 @@ void ImGui_ImplSdlGL2_RenderDrawData(ImDrawData* draw_data) static const char* ImGui_ImplSdlGL2_GetClipboardText(void*) { - return SDL_GetClipboardText(); + if (g_ClipboardTextData) SDL_free(g_ClipboardTextData); + g_ClipboardTextData = SDL_GetClipboardText(); + + return g_ClipboardTextData; } static void ImGui_ImplSdlGL2_SetClipboardText(void*, const char* text) @@ -285,6 +289,9 @@ void ImGui_ImplSdlGL2_Shutdown() SDL_FreeCursor(g_MouseCursors[cursor_n]); memset(g_MouseCursors, 0, sizeof(g_MouseCursors)); + // Remove previously allocated clipboard text data + if (g_ClipboardTextData) SDL_free(g_ClipboardTextData); + // Destroy OpenGL objects ImGui_ImplSdlGL2_InvalidateDeviceObjects(); } diff --git a/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp b/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp index 37c5d209..0a25d5f2 100644 --- a/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp +++ b/examples/sdl_opengl3_example/imgui_impl_sdl_gl3.cpp @@ -51,6 +51,7 @@ static Uint64 g_Time = 0; static bool g_MousePressed[3] = { false, false, false }; static SDL_Cursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = { 0 }; +static char* g_ClipboardTextData = NULL; // OpenGL data static char g_GlslVersion[32] = "#version 150"; @@ -183,7 +184,10 @@ void ImGui_ImplSdlGL3_RenderDrawData(ImDrawData* draw_data) static const char* ImGui_ImplSdlGL3_GetClipboardText(void*) { - return SDL_GetClipboardText(); + if (g_ClipboardTextData) SDL_free(g_ClipboardTextData); + g_ClipboardTextData = SDL_GetClipboardText(); + + return g_ClipboardTextData; } static void ImGui_ImplSdlGL3_SetClipboardText(void*, const char* text) @@ -418,6 +422,9 @@ void ImGui_ImplSdlGL3_Shutdown() SDL_FreeCursor(g_MouseCursors[cursor_n]); memset(g_MouseCursors, 0, sizeof(g_MouseCursors)); + // Remove previously allocated clipboard text data + if (g_ClipboardTextData) SDL_free(g_ClipboardTextData); + // Destroy OpenGL objects ImGui_ImplSdlGL3_InvalidateDeviceObjects(); }