From 3e4af3cd8c7d4e09f6074f475727e0cbeb297462 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Thu, 9 Oct 2014 12:52:05 +0000 Subject: [PATCH] Make sure we unregister for clipboard notifications on exit on Windows. This is necessary because Windows doesn't implicitly clean up when a process dies, and we cannot trust applications to always explicitly unregister. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@10367 ea41ed52-d2ee-0310-a9c1-e6b18d33e121 --- src/Fl_win32.cxx | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Fl_win32.cxx b/src/Fl_win32.cxx index fe051e363..0872981f5 100644 --- a/src/Fl_win32.cxx +++ b/src/Fl_win32.cxx @@ -472,6 +472,11 @@ public: OleUninitialize(); fl_brush_action(1); fl_cleanup_dc_list(); + // This is actually too late in the cleanup process to remove the + // clipboard notifications, but we have no earlier hook so we try + // to work around it anyway. + if (clipboard_wnd != NULL) + fl_clipboard_notify_untarget(clipboard_wnd); } }; static Fl_Win32_At_Exit win32_at_exit; @@ -812,7 +817,30 @@ static void fl_clipboard_notify_untarget(HWND wnd) { if (wnd != clipboard_wnd) return; - ChangeClipboardChain(wnd, next_clipboard_wnd); + // We might be called late in the cleanup where Windows has already + // implicitly destroyed our clipboard window. At that point we need + // to do some extra work to manually repair the clipboard chain. + if (IsWindow(wnd)) + ChangeClipboardChain(wnd, next_clipboard_wnd); + else { + HWND tmp, head; + + tmp = CreateWindow("STATIC", "Temporary FLTK Clipboard Window", 0, + 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL); + if (tmp == NULL) + return; + + head = SetClipboardViewer(tmp); + if (head == NULL) + ChangeClipboardChain(tmp, next_clipboard_wnd); + else { + SendMessage(head, WM_CHANGECBCHAIN, (WPARAM)wnd, (LPARAM)next_clipboard_wnd); + ChangeClipboardChain(tmp, head); + } + + DestroyWindow(tmp); + } + clipboard_wnd = next_clipboard_wnd = 0; }