From b1698e9bc2ca0668f5b5f0e7af2274eb2a0cb7e0 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 4 Sep 2019 23:44:16 +0200 Subject: QWinRTUia*: remove anti-pattern passing new'ed QSharedPointer into lambdas MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QSharedPointer is made for passing around by value. It is unclear why in so many repeated cases a QSharedPointer is created on the stack, then a copy of it is new'ed up, passed into a lambda to be deleted inside it. First, it requires an additional heap allocation. Because it's passed as a raw pointer to QSharedPointer, however, there's also always the danger that it's leaked by seemingly-innocuous changes such as adding an early return from the lambda (and some of them could really use one, with the ifs nesting several levels deep). All this is not needed, though. It's perfectly ok to store a copy of a QSharedPointer, by value, in a lambda, and keep one copy outside it. Poor man's std::future, if you will. So, do away with all that, just pass the shared pointer by value into the lambda, and, as a drive-by, replace some ephemeral QLists with QVLAs. In one case, replace a QPair with a struct to make the code using that type more accessible ('first' and 'second' are really, really bad variable names if they, in fact, represent 'startOffset' and 'endOffset'). Also port directly to shared_ptr / make_shared. Saves one memory allocation each, due to the co-allocation of payload and control block, and even though there's QSharedPointer::create, which does this, too, std::shared_ptr is simply much lighter on the use of atomics (copying a QSP ups two ref counts, copying a std::shared_ptr just one). Since these variables live behind the API boundary, there's no reason not to prefer the more efficient alternative. Change-Id: I4b9fe30e56df5106fc2ab7a0b55b2b8316cca5fe Reviewed-by: Oliver Wolff Reviewed-by: MÃ¥rten Nordheim --- .../winrt/uiautomation/qwinrtuiaselectionitemprovider.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/plugins/platforms/winrt/uiautomation/qwinrtuiaselectionitemprovider.cpp') diff --git a/src/plugins/platforms/winrt/uiautomation/qwinrtuiaselectionitemprovider.cpp b/src/plugins/platforms/winrt/uiautomation/qwinrtuiaselectionitemprovider.cpp index 9bc88272ba..2cb5aa685c 100644 --- a/src/plugins/platforms/winrt/uiautomation/qwinrtuiaselectionitemprovider.cpp +++ b/src/plugins/platforms/winrt/uiautomation/qwinrtuiaselectionitemprovider.cpp @@ -51,6 +51,8 @@ #include #include +#include + QT_BEGIN_NAMESPACE using namespace QWinRTUiAutomation; @@ -94,21 +96,19 @@ HRESULT STDMETHODCALLTYPE QWinRTUiaSelectionItemProvider::get_SelectionContainer *value = nullptr; auto accid = id(); - auto elementId = QSharedPointer(new QAccessible::Id(0)); - auto ptrElementId = new QSharedPointer(elementId); + auto elementId = std::make_shared(0); - if (!SUCCEEDED(QEventDispatcherWinRT::runOnMainThread([accid, ptrElementId]() { + if (!SUCCEEDED(QEventDispatcherWinRT::runOnMainThread([accid, elementId]() { if (QAccessibleInterface *accessible = accessibleForId(accid)) { // Radio buttons do not require a container. if (accessible->role() == QAccessible::ListItem) { if (QAccessibleInterface *parent = accessible->parent()) { if (parent->role() == QAccessible::List) { - **ptrElementId = idForAccessible(parent); + *elementId = idForAccessible(parent); } } } } - delete ptrElementId; return S_OK; }))) { return E_FAIL; -- cgit v1.2.3