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 <fabian.kosmale@qt.io>
This commit is contained in:
Ulf Hermann 2024-03-01 14:22:47 +01:00
parent 7071010880
commit 476e503cfb
2 changed files with 10 additions and 6 deletions

View File

@ -27,9 +27,9 @@ void QPropertyBindingPrivatePtr::reset(QtPrivate::RefCounted *ptr) noexcept
{ {
if (ptr != d) { if (ptr != d) {
if (ptr) if (ptr)
ptr->ref++; ptr->addRef();
auto *old = std::exchange(d, ptr); auto *old = std::exchange(d, ptr);
if (old && (--old->ref == 0)) if (old && !old->deref())
QPropertyBindingPrivate::destroyAndFreeMemory(static_cast<QPropertyBindingPrivate *>(d)); QPropertyBindingPrivate::destroyAndFreeMemory(static_cast<QPropertyBindingPrivate *>(d));
} }
} }
@ -310,7 +310,7 @@ void QPropertyBindingPrivate::unlinkAndDeref()
{ {
clearDependencyObservers(); clearDependencyObservers();
propertyDataPtr = nullptr; propertyDataPtr = nullptr;
if (--ref == 0) if (!deref())
destroyAndFreeMemory(this); destroyAndFreeMemory(this);
} }

View File

@ -36,9 +36,13 @@ namespace QtPrivate {
// QPropertyBindingPrivatePtr operates on a RefCountingMixin solely so that we can inline // QPropertyBindingPrivatePtr operates on a RefCountingMixin solely so that we can inline
// the constructor and copy constructor // the constructor and copy constructor
struct RefCounted { struct RefCounted {
int refCount() const { return ref; }
void addRef() { ++ref; }
bool deref() { return --ref != 0; }
private:
int ref = 0; int ref = 0;
void addRef() {++ref;}
bool deref() {--ref; return ref;}
}; };
} }
@ -61,7 +65,7 @@ public:
QPropertyBindingPrivatePtr() noexcept : d(nullptr) { } QPropertyBindingPrivatePtr() noexcept : d(nullptr) { }
~QPropertyBindingPrivatePtr() ~QPropertyBindingPrivatePtr()
{ {
if (d && (--d->ref == 0)) if (d && !d->deref())
destroyAndFreeMemory(); destroyAndFreeMemory();
} }
Q_CORE_EXPORT void destroyAndFreeMemory(); Q_CORE_EXPORT void destroyAndFreeMemory();