QObjCWeakPointer: Clear existing associated object on assignment

If the existing m_object is the same as the incoming object the
call to objc_setAssociatedObject in trackObjectLifetime will
release the existing WeakPointerLifetimeTracker after assigning
a new one, which means we'd clear the QObjCWeakPointer's object.

We now reset the state up front.

Fixes: QTBUG-132256
Pick-to: 6.8 6.9
Change-Id: If2c08840d465ae6d190c87a4720a537fe9caa8dc
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
Tor Arne Vestbø 2024-12-16 16:05:20 +01:00
parent a56a6ba22d
commit 92012333d1

View File

@ -504,6 +504,9 @@ public:
QObjCWeakPointer &operator=(const QObjCWeakPointer &other) QObjCWeakPointer &operator=(const QObjCWeakPointer &other)
{ {
QMacAutoReleasePool pool; QMacAutoReleasePool pool;
#if !USE_OBJC_WEAK
objc_setAssociatedObject(m_object, this, nil, OBJC_ASSOCIATION_RETAIN);
#endif
m_object = other.m_object; m_object = other.m_object;
#if !USE_OBJC_WEAK #if !USE_OBJC_WEAK
trackObjectLifetime(); trackObjectLifetime();