diff options
author | Neil <nyamatongwe@gmail.com> | 2020-06-06 13:41:58 +1000 |
---|---|---|
committer | Neil <nyamatongwe@gmail.com> | 2020-06-06 13:41:58 +1000 |
commit | 3a8d7270b2eeaab9884eacc012ec968ba0b2da06 (patch) | |
tree | 733529258b61908315d39fbceeff54406c6f075b | |
parent | 1be0995e0ee4ec69426ae908ae8d05603dfaa905 (diff) | |
download | scintilla-mirror-3a8d7270b2eeaab9884eacc012ec968ba0b2da06.tar.gz |
Add ReleaseUnknown to safely release IUnknown* and avoid warnings when done in
noexcept context.
-rw-r--r-- | win32/PlatWin.cxx | 16 | ||||
-rw-r--r-- | win32/PlatWin.h | 17 | ||||
-rw-r--r-- | win32/ScintillaWin.cxx | 8 |
3 files changed, 23 insertions, 18 deletions
diff --git a/win32/PlatWin.cxx b/win32/PlatWin.cxx index 60bdbfee6..73f85e0a9 100644 --- a/win32/PlatWin.cxx +++ b/win32/PlatWin.cxx @@ -194,9 +194,7 @@ struct FormatAndMetrics { if (hfont) ::DeleteObject(hfont); #if defined(USE_D2D) - if (pTextFormat) - pTextFormat->Release(); - pTextFormat = nullptr; + ReleaseUnknown(pTextFormat); #endif extraFontFlag = 0; characterSet = 0; @@ -1154,10 +1152,7 @@ SurfaceD2D::~SurfaceD2D() { } void SurfaceD2D::Clear() noexcept { - if (pBrush) { - pBrush->Release(); - pBrush = nullptr; - } + ReleaseUnknown(pBrush); if (pRenderTarget) { while (clipsActive) { pRenderTarget->PopAxisAlignedClip(); @@ -1165,7 +1160,7 @@ void SurfaceD2D::Clear() noexcept { } if (ownRenderTarget) { pRenderTarget->EndDraw(); - pRenderTarget->Release(); + ReleaseUnknown(pRenderTarget); ownRenderTarget = false; } pRenderTarget = nullptr; @@ -1788,10 +1783,7 @@ ScreenLineLayout::ScreenLineLayout(const IScreenLine *screenLine) { } ScreenLineLayout::~ScreenLineLayout() { - if (textLayout) { - textLayout->Release(); - textLayout = nullptr; - } + ReleaseUnknown(textLayout); } // Get the position from the provided x diff --git a/win32/PlatWin.h b/win32/PlatWin.h index 58ba5e78f..0d1c99b48 100644 --- a/win32/PlatWin.h +++ b/win32/PlatWin.h @@ -57,6 +57,23 @@ T DLLFunction(HMODULE hModule, LPCSTR lpProcName) noexcept { return fp; } +// Release an IUnknown* and set to nullptr. +// While IUnknown::Release must be noexcept, it isn't marked as such so produces +// warnings which are avoided by the catch. +template <class T> +void ReleaseUnknown(T *&ppUnknown) noexcept { + if (ppUnknown) { + try { + ppUnknown->Release(); + } + catch (...) { + // Never occurs + } + ppUnknown = nullptr; + } +} + + UINT DpiForWindow(WindowID wid) noexcept; int SystemMetricsForDpi(int nIndex, UINT dpi) noexcept; diff --git a/win32/ScintillaWin.cxx b/win32/ScintillaWin.cxx index 916185569..38fa351c0 100644 --- a/win32/ScintillaWin.cxx +++ b/win32/ScintillaWin.cxx @@ -678,10 +678,7 @@ void ScintillaWin::EnsureRenderTarget(HDC hdc) { } void ScintillaWin::DropRenderTarget() { - if (pRenderTarget) { - pRenderTarget->Release(); - pRenderTarget = nullptr; - } + ReleaseUnknown(pRenderTarget); } #endif @@ -3469,8 +3466,7 @@ LRESULT PASCAL ScintillaWin::CTWndProc( #endif surfaceWindow->Release(); #if defined(USE_D2D) - if (pCTRenderTarget) - pCTRenderTarget->Release(); + ReleaseUnknown(pCTRenderTarget); #endif ::EndPaint(hWnd, &ps); return 0; |