Fixed Windows default clipboard leaving its buffer unfreed on application's exit. (#714)

docking
ocornut 9 years ago
parent de61fcc619
commit e06852abaf

@ -9501,12 +9501,8 @@ void ImGui::ValueColor(const char* prefix, ImU32 v)
static const char* GetClipboardTextFn_DefaultImpl() static const char* GetClipboardTextFn_DefaultImpl()
{ {
static char* buf_local = NULL; static ImVector<char> buf_local;
if (buf_local) buf_local.clear();
{
ImGui::MemFree(buf_local);
buf_local = NULL;
}
if (!OpenClipboard(NULL)) if (!OpenClipboard(NULL))
return NULL; return NULL;
HANDLE wbuf_handle = GetClipboardData(CF_UNICODETEXT); HANDLE wbuf_handle = GetClipboardData(CF_UNICODETEXT);
@ -9515,19 +9511,18 @@ static const char* GetClipboardTextFn_DefaultImpl()
if (ImWchar* wbuf_global = (ImWchar*)GlobalLock(wbuf_handle)) if (ImWchar* wbuf_global = (ImWchar*)GlobalLock(wbuf_handle))
{ {
int buf_len = ImTextCountUtf8BytesFromStr(wbuf_global, NULL) + 1; int buf_len = ImTextCountUtf8BytesFromStr(wbuf_global, NULL) + 1;
buf_local = (char*)ImGui::MemAlloc(buf_len * sizeof(char)); buf_local.resize(buf_len);
ImTextStrToUtf8(buf_local, buf_len, wbuf_global, NULL); ImTextStrToUtf8(buf_local.Data, buf_len, wbuf_global, NULL);
} }
GlobalUnlock(wbuf_handle); GlobalUnlock(wbuf_handle);
CloseClipboard(); CloseClipboard();
return buf_local; return buf_local.Data;
} }
static void SetClipboardTextFn_DefaultImpl(const char* text) static void SetClipboardTextFn_DefaultImpl(const char* text)
{ {
if (!OpenClipboard(NULL)) if (!OpenClipboard(NULL))
return; return;
const int wbuf_length = ImTextCountCharsFromUtf8(text, NULL) + 1; const int wbuf_length = ImTextCountCharsFromUtf8(text, NULL) + 1;
HGLOBAL wbuf_handle = GlobalAlloc(GMEM_MOVEABLE, (SIZE_T)wbuf_length * sizeof(ImWchar)); HGLOBAL wbuf_handle = GlobalAlloc(GMEM_MOVEABLE, (SIZE_T)wbuf_length * sizeof(ImWchar));
if (wbuf_handle == NULL) if (wbuf_handle == NULL)

Loading…
Cancel
Save