diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2019-09-04 23:44:16 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2019-09-10 12:51:51 +0200 |
commit | b1698e9bc2ca0668f5b5f0e7af2274eb2a0cb7e0 (patch) | |
tree | 461d51568b599e46b2dcd9f6b5678ac08599281d /src/plugins/platforms/winrt/uiautomation/qwinrtuiagridprovider.cpp | |
parent | c0b3c06a7d4f6a2f90742e8dc81c898082a70416 (diff) |
QWinRTUia*: remove anti-pattern passing new'ed QSharedPointer into lambdas
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<int, int> 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 <oliver.wolff@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/plugins/platforms/winrt/uiautomation/qwinrtuiagridprovider.cpp')
-rw-r--r-- | src/plugins/platforms/winrt/uiautomation/qwinrtuiagridprovider.cpp | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/src/plugins/platforms/winrt/uiautomation/qwinrtuiagridprovider.cpp b/src/plugins/platforms/winrt/uiautomation/qwinrtuiagridprovider.cpp index e469991de2..3bd90f6850 100644 --- a/src/plugins/platforms/winrt/uiautomation/qwinrtuiagridprovider.cpp +++ b/src/plugins/platforms/winrt/uiautomation/qwinrtuiagridprovider.cpp @@ -51,6 +51,8 @@ #include <QtCore/QString> #include <QtCore/private/qeventdispatcher_winrt_p.h> +#include <memory> + QT_BEGIN_NAMESPACE using namespace QWinRTUiAutomation; @@ -104,21 +106,19 @@ HRESULT STDMETHODCALLTYPE QWinRTUiaGridProvider::GetItem(INT32 row, INT32 column *returnValue = nullptr; auto accid = id(); - auto elementId = QSharedPointer<QAccessible::Id>(new QAccessible::Id(0)); - auto ptrElementId = new QSharedPointer<QAccessible::Id>(elementId); + auto elementId = std::make_shared<QAccessible::Id>(0); - if (!SUCCEEDED(QEventDispatcherWinRT::runOnMainThread([accid, row, column, ptrElementId]() { + if (!SUCCEEDED(QEventDispatcherWinRT::runOnMainThread([accid, row, column, elementId]() { if (QAccessibleInterface *accessible = accessibleForId(accid)) { if (QAccessibleTableInterface *tableInterface = accessible->tableInterface()) { if ((row >= 0) && (row < tableInterface->rowCount()) && (column >= 0) && (column < tableInterface->columnCount())) { if (QAccessibleInterface *cell = tableInterface->cellAt(row, column)) { - **ptrElementId = idForAccessible(cell); - QWinRTUiaMetadataCache::instance()->load(**ptrElementId); + *elementId = idForAccessible(cell); + QWinRTUiaMetadataCache::instance()->load(*elementId); } } } } - delete ptrElementId; return S_OK; }))) { return E_FAIL; |