From b528e44f5a9fc3194f4d2a14fee1e56558061997 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Mon, 13 Feb 2023 18:41:56 +0100 Subject: [PATCH] QWindowsXPStyle: use QVarLengthArray instead new for memory allocation Avoid the creation of a temporary buffer by using QVarLengthArray instead. Normally GetRegionData() only returns one rect (~48bytes) so it will fit into the QVLA without a memory allocation Change-Id: I279693e17e2f9f2c1c75504c3e5c1de3d45084ec Reviewed-by: Volker Hilsheimer --- .../windowsvista/qwindowsvistastyle.cpp | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/plugins/styles/windowsvista/qwindowsvistastyle.cpp b/src/plugins/styles/windowsvista/qwindowsvistastyle.cpp index e90d1c0925..4ae2adde35 100644 --- a/src/plugins/styles/windowsvista/qwindowsvistastyle.cpp +++ b/src/plugins/styles/windowsvista/qwindowsvistastyle.cpp @@ -504,29 +504,29 @@ QRegion QWindowsVistaStylePrivate::region(QWindowsThemeData &themeData) QRegion region; if (success) { - const auto numBytes = GetRegionData(dest, 0, nullptr); - if (numBytes == 0) - return QRegion(); - - char *buf = new (std::nothrow) char[numBytes]; - if (!buf) - return QRegion(); - - RGNDATA *rd = reinterpret_cast(buf); - if (GetRegionData(dest, numBytes, rd) == 0) { - delete [] buf; - return QRegion(); + QVarLengthArray buf(256); + RGNDATA *rd = reinterpret_cast(buf.data()); + if (GetRegionData(dest, buf.size(), rd) == 0) { + const auto numBytes = GetRegionData(dest, 0, nullptr); + if (numBytes > 0) { + buf.resize(numBytes); + rd = reinterpret_cast(buf.data()); + if (GetRegionData(dest, numBytes, rd) == 0) + rd = nullptr; + } else { + rd = nullptr; + } } - - RECT *r = reinterpret_cast(rd->Buffer); - for (uint i = 0; i < rd->rdh.nCount; ++i) { - QRect rect; - rect.setCoords(int(r->left * factor), int(r->top * factor), int((r->right - 1) * factor), int((r->bottom - 1) * factor)); - ++r; - region |= rect; + if (rd) { + RECT *r = reinterpret_cast(rd->Buffer); + for (uint i = 0; i < rd->rdh.nCount; ++i) { + QRect rect; + rect.setCoords(int(r->left * factor), int(r->top * factor), + int((r->right - 1) * factor), int((r->bottom - 1) * factor)); + ++r; + region |= rect; + } } - - delete [] buf; } DeleteObject(hRgn);