From fb70d90fef176b24075bbaeb406612ab5f39fec6 Mon Sep 17 00:00:00 2001 From: omar Date: Thu, 9 Apr 2020 13:34:39 +0200 Subject: [PATCH] Made default clipboard handlers for Win32 and OSX use a buffer inside the main context instead of a static buffer, so it can be freed properly on Shutdown. (#3110) --- docs/CHANGELOG.txt | 2 ++ imgui.cpp | 34 ++++++++++++++++++---------------- imgui_internal.h | 2 +- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 6bdb7512b..f274ba31d 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -73,6 +73,8 @@ Other Changes: to a solution rather than encourage people to add braces in the codebase. - Misc: Added additional checks in EndFrame() to verify that io.KeyXXX values have not been tampered with between NewFrame() and EndFrame(). +- Misc: Made default clipboard handlers for Win32 and OSX use a buffer inside the main context + instead of a static buffer, so it can be freed properly on Shutdown. (#3110) - Misc, Freetype: Fixed support for IMGUI_STB_RECT_PACK_FILENAME compile time directive in imgui_freetype.cpp (matching support in the regular code path). (#3062) [@DonKult] - Metrics: Made Tools section more prominent. Showing wire-frame mesh directly hovering the ImDrawCmd diff --git a/imgui.cpp b/imgui.cpp index 42a7228eb..2aaddb67c 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -3985,7 +3985,7 @@ void ImGui::Shutdown(ImGuiContext* context) g.CurrentTabBarStack.clear(); g.ShrinkWidthBuffer.clear(); - g.PrivateClipboard.clear(); + g.ClipboardHandlerData.clear(); g.MenusIdSubmittedThisFrame.clear(); g.InputTextState.ClearFreeMemory(); @@ -9805,10 +9805,11 @@ static void WindowSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandl #endif // Win32 clipboard implementation +// We use g.ClipboardHandlerData for temporary storage to ensure it is freed on Shutdown() static const char* GetClipboardTextFn_DefaultImpl(void*) { - static ImVector buf_local; - buf_local.clear(); + ImGuiContext& g = *GImGui; + g.ClipboardHandlerData.clear(); if (!::OpenClipboard(NULL)) return NULL; HANDLE wbuf_handle = ::GetClipboardData(CF_UNICODETEXT); @@ -9820,12 +9821,12 @@ static const char* GetClipboardTextFn_DefaultImpl(void*) if (const WCHAR* wbuf_global = (const WCHAR*)::GlobalLock(wbuf_handle)) { int buf_len = ::WideCharToMultiByte(CP_UTF8, 0, wbuf_global, -1, NULL, 0, NULL, NULL); - buf_local.resize(buf_len); - ::WideCharToMultiByte(CP_UTF8, 0, wbuf_global, -1, buf_local.Data, buf_len, NULL, NULL); + g.ClipboardHandlerData.resize(buf_len); + ::WideCharToMultiByte(CP_UTF8, 0, wbuf_global, -1, g.ClipboardHandlerData.Data, buf_len, NULL, NULL); } ::GlobalUnlock(wbuf_handle); ::CloseClipboard(); - return buf_local.Data; + return g.ClipboardHandlerData.Data; } static void SetClipboardTextFn_DefaultImpl(void*, const char* text) @@ -9887,13 +9888,14 @@ static const char* GetClipboardTextFn_DefaultImpl(void*) CFDataRef cf_data; if (PasteboardCopyItemFlavorData(main_clipboard, item_id, CFSTR("public.utf8-plain-text"), &cf_data) == noErr) { - static ImVector clipboard_text; + ImGuiContext& g = *GImGui; + g.ClipboardHandlerData.clear(); int length = (int)CFDataGetLength(cf_data); - clipboard_text.resize(length + 1); - CFDataGetBytes(cf_data, CFRangeMake(0, length), (UInt8*)clipboard_text.Data); - clipboard_text[length] = 0; + g.ClipboardHandlerData.resize(length + 1); + CFDataGetBytes(cf_data, CFRangeMake(0, length), (UInt8*)g.ClipboardHandlerData.Data); + g.ClipboardHandlerData[length] = 0; CFRelease(cf_data); - return clipboard_text.Data; + return g.ClipboardHandlerData.Data; } } } @@ -9906,17 +9908,17 @@ static const char* GetClipboardTextFn_DefaultImpl(void*) static const char* GetClipboardTextFn_DefaultImpl(void*) { ImGuiContext& g = *GImGui; - return g.PrivateClipboard.empty() ? NULL : g.PrivateClipboard.begin(); + return g.ClipboardHandlerData.empty() ? NULL : g.ClipboardHandlerData.begin(); } static void SetClipboardTextFn_DefaultImpl(void*, const char* text) { ImGuiContext& g = *GImGui; - g.PrivateClipboard.clear(); + g.ClipboardHandlerData.clear(); const char* text_end = text + strlen(text); - g.PrivateClipboard.resize((int)(text_end - text) + 1); - memcpy(&g.PrivateClipboard[0], text, (size_t)(text_end - text)); - g.PrivateClipboard[(int)(text_end - text)] = 0; + g.ClipboardHandlerData.resize((int)(text_end - text) + 1); + memcpy(&g.ClipboardHandlerData[0], text, (size_t)(text_end - text)); + g.ClipboardHandlerData[(int)(text_end - text)] = 0; } #endif diff --git a/imgui_internal.h b/imgui_internal.h index 2172c1380..7e744b3f7 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -1189,7 +1189,7 @@ struct ImGuiContext float DragSpeedDefaultRatio; // If speed == 0.0f, uses (max-min) * DragSpeedDefaultRatio float ScrollbarClickDeltaToGrabCenter; // Distance between mouse and center of grab box, normalized in parent space. Use storage? int TooltipOverrideCount; - ImVector PrivateClipboard; // If no custom clipboard handler is defined + ImVector ClipboardHandlerData; // If no custom clipboard handler is defined ImVector MenusIdSubmittedThisFrame; // A list of menu IDs that were rendered at least once // Platform support