diff options
author | Neil <nyamatongwe@gmail.com> | 2024-06-29 09:36:53 +1000 |
---|---|---|
committer | Neil <nyamatongwe@gmail.com> | 2024-06-29 09:36:53 +1000 |
commit | 2b69fbaf70f6a05269d96b2703acfe49ef7f03a1 (patch) | |
tree | 4ce37e3878a26e65193c539c042dd94da7945284 | |
parent | e1526b995aba2e8cd7b4ee018abaaf62e9897e06 (diff) | |
download | scintilla-mirror-2b69fbaf70f6a05269d96b2703acfe49ef7f03a1.tar.gz |
Ensure clipboard is closed even if exception occurs while clipboard open.
-rw-r--r-- | win32/ScintillaWin.cxx | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/win32/ScintillaWin.cxx b/win32/ScintillaWin.cxx index 31d1d826f..748560a10 100644 --- a/win32/ScintillaWin.cxx +++ b/win32/ScintillaWin.cxx @@ -2749,6 +2749,27 @@ bool OpenClipboardRetry(HWND hwnd) noexcept { return false; } +// Ensure every successful OpenClipboard is followed by a CloseClipboard. +class Clipboard { + bool opened = false; +public: + Clipboard(HWND hwnd) noexcept : opened(::OpenClipboardRetry(hwnd)) { + } + // Deleted so Clipboard objects can not be copied. + Clipboard(const Clipboard &) = delete; + Clipboard(Clipboard &&) = delete; + Clipboard &operator=(const Clipboard &) = delete; + Clipboard &operator=(Clipboard &&) = delete; + ~Clipboard() noexcept { + if (opened) { + ::CloseClipboard(); + } + } + constexpr operator bool() const noexcept { + return opened; + } +}; + bool IsValidFormatEtc(const FORMATETC *pFE) noexcept { return pFE->ptd == nullptr && (pFE->dwAspect & DVASPECT_CONTENT) != 0 && @@ -2764,7 +2785,8 @@ bool SupportedFormat(const FORMATETC *pFE) noexcept { } void ScintillaWin::Paste() { - if (!::OpenClipboardRetry(MainHWND())) { + Clipboard clipboard(MainHWND()); + if (!clipboard) { return; } UndoGroup ug(pdoc); @@ -2790,7 +2812,6 @@ void ScintillaWin::Paste() { InsertPasteShape(putf.c_str(), putf.length(), pasteShape); memUSelection.Unlock(); } - ::CloseClipboard(); Redraw(); } @@ -3254,7 +3275,8 @@ void ScintillaWin::CopyToGlobal(GlobalMemory &gmUnicode, const SelectionText &se } void ScintillaWin::CopyToClipboard(const SelectionText &selectedText) { - if (!::OpenClipboardRetry(MainHWND())) { + Clipboard clipboard(MainHWND()); + if (!clipboard) { return; } ::EmptyClipboard(); @@ -3280,8 +3302,6 @@ void ScintillaWin::CopyToClipboard(const SelectionText &selectedText) { ::SetClipboardData(cfLineSelect, 0); ::SetClipboardData(cfVSLineTag, 0); } - - ::CloseClipboard(); } void ScintillaWin::ScrollMessage(WPARAM wParam) { |