From 476e503cfbc42b8ea9e94a1f536d443fc8ce69c0 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Fri, 1 Mar 2024 14:22:47 +0100 Subject: [PATCH] QProperty: Use RefCounted as intended You're not supposed to mess with the refcount directly. That's what the addRef() and deref() methods are for. Change-Id: I5cae946cef7ac72dd99b247ade95590d142c269e Reviewed-by: Fabian Kosmale --- src/corelib/kernel/qproperty.cpp | 6 +++--- src/corelib/kernel/qpropertyprivate.h | 10 +++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/corelib/kernel/qproperty.cpp b/src/corelib/kernel/qproperty.cpp index 4819ac4d57b..caa9fce787b 100644 --- a/src/corelib/kernel/qproperty.cpp +++ b/src/corelib/kernel/qproperty.cpp @@ -27,9 +27,9 @@ void QPropertyBindingPrivatePtr::reset(QtPrivate::RefCounted *ptr) noexcept { if (ptr != d) { if (ptr) - ptr->ref++; + ptr->addRef(); auto *old = std::exchange(d, ptr); - if (old && (--old->ref == 0)) + if (old && !old->deref()) QPropertyBindingPrivate::destroyAndFreeMemory(static_cast(d)); } } @@ -310,7 +310,7 @@ void QPropertyBindingPrivate::unlinkAndDeref() { clearDependencyObservers(); propertyDataPtr = nullptr; - if (--ref == 0) + if (!deref()) destroyAndFreeMemory(this); } diff --git a/src/corelib/kernel/qpropertyprivate.h b/src/corelib/kernel/qpropertyprivate.h index 28cc04ff9d5..86dc08a6bc0 100644 --- a/src/corelib/kernel/qpropertyprivate.h +++ b/src/corelib/kernel/qpropertyprivate.h @@ -36,9 +36,13 @@ namespace QtPrivate { // QPropertyBindingPrivatePtr operates on a RefCountingMixin solely so that we can inline // the constructor and copy constructor struct RefCounted { + + int refCount() const { return ref; } + void addRef() { ++ref; } + bool deref() { return --ref != 0; } + +private: int ref = 0; - void addRef() {++ref;} - bool deref() {--ref; return ref;} }; } @@ -61,7 +65,7 @@ public: QPropertyBindingPrivatePtr() noexcept : d(nullptr) { } ~QPropertyBindingPrivatePtr() { - if (d && (--d->ref == 0)) + if (d && !d->deref()) destroyAndFreeMemory(); } Q_CORE_EXPORT void destroyAndFreeMemory();