summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2019-05-17 11:42:53 +0200
committerMarc Mutz <marc.mutz@kdab.com>2019-06-05 14:09:14 +0200
commited17fe7fa22d6ce6a9437a19b4baa39528cee1df (patch)
tree341a9097aa97de66eacac82bdc24364531ae2c9f
parentc5e889bf90b255db3597cbcb5e21e8843f4b5365 (diff)
Eradicate Q_FOREACH loops [2/2]: non-trivial cases
This patch contains two changes where there is a non-neglible chance that through some indirect call the container we iterate over could be modified. In the listener case, it's not impossible that a listener de-registers when it's notified. So take a copy. In the polish_objects case, the 'initialized' flag is set before the loop starts, and the only other reference to polish_objects, in addPolishObject(), does not append to polish_objects in that case, but sends the polish event directly. So use a consume loop, to release the memory and the QPointers once they're dealt with. Change-Id: I63d32e8a298ed5e1a7d5446434c421df80c0c795 Reviewed-by: Johan Helsing <johan.helsing@qt.io>
-rw-r--r--src/client/qwaylanddisplay.cpp3
-rw-r--r--src/compositor/compositor_api/qwaylandcompositor.cpp2
2 files changed, 3 insertions, 2 deletions
diff --git a/src/client/qwaylanddisplay.cpp b/src/client/qwaylanddisplay.cpp
index 53f5cfcea..cb4f190ad 100644
--- a/src/client/qwaylanddisplay.cpp
+++ b/src/client/qwaylanddisplay.cpp
@@ -346,7 +346,8 @@ void QWaylandDisplay::registry_global(uint32_t id, const QString &interface, uin
mGlobals.append(RegistryGlobal(id, interface, version, registry));
- foreach (Listener l, mRegistryListeners)
+ const auto copy = mRegistryListeners; // be prepared for listeners unregistering on notification
+ for (Listener l : copy)
(*l.listener)(l.data, registry, id, interface, version);
}
diff --git a/src/compositor/compositor_api/qwaylandcompositor.cpp b/src/compositor/compositor_api/qwaylandcompositor.cpp
index 36c995c3d..2d10c6384 100644
--- a/src/compositor/compositor_api/qwaylandcompositor.cpp
+++ b/src/compositor/compositor_api/qwaylandcompositor.cpp
@@ -234,7 +234,7 @@ void QWaylandCompositorPrivate::init()
initialized = true;
- Q_FOREACH (QPointer<QObject> object, polish_objects) {
+ for (const QPointer<QObject> &object : qExchange(polish_objects, {})) {
if (object) {
QEvent polishEvent(QEvent::Polish);
QCoreApplication::sendEvent(object.data(), &polishEvent);