Use QObjCWeakPointer to track QMacKeyValueObserver observed object

Otherwise we might run the risk that the observed object dies before
we try to do removeObserver.

Change-Id: I2b54ff32728fc40fd47be2d14070fc43640f81bf
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit b6ffbebbbe75c53f8dd9a7afd97032f7f4fd8f5c)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tor Arne Vestbø 2025-04-08 21:48:55 +02:00 committed by Qt Cherry-pick Bot
parent 2a967f5c99
commit fe148dc641

View File

@ -408,7 +408,6 @@ public:
QMacKeyValueObserver() = default;
#if defined( __OBJC__)
// Note: QMacKeyValueObserver must not outlive the object observed!
QMacKeyValueObserver(NSObject *object, NSString *keyPath, Callback callback,
NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew)
: object(object), keyPath(keyPath), callback(new Callback(callback))
@ -436,7 +435,7 @@ public:
void swap(QMacKeyValueObserver &other) noexcept
{
qt_ptr_swap(object, other.object);
std::swap(object, other.object);
qt_ptr_swap(keyPath, other.keyPath);
callback.swap(other.callback);
}
@ -446,7 +445,7 @@ private:
void addObserver(NSKeyValueObservingOptions options);
#endif
NSObject *object = nullptr;
QObjCWeakPointer<NSObject> object;
NSString *keyPath = nullptr;
std::unique_ptr<Callback> callback;