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
Change-Id: If2c08840d465ae6d190c87a4720a537fe9caa8dc
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
(cherry picked from commit 92012333d112dbdf2926117ec3bb123bd30ed9fc)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit c8ae8244b431e20e63eb8c6934b5c814c1e33582)
This commit is contained in:
Tor Arne Vestbø 2024-12-16 16:05:20 +01:00 committed by Qt Cherry-pick Bot
parent 3beb1f5dd8
commit 13db433219

View File

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