Use QObjCWeakPointer to track QMacKeyValueObserver observed object

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

Pick-to: 6.9
Change-Id: I2b54ff32728fc40fd47be2d14070fc43640f81bf
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Tor Arne Vestbø 2025-04-08 21:48:55 +02:00
parent 1c98b3fe40
commit b6ffbebbbe

View File

@ -407,7 +407,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))
@ -435,7 +434,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);
}
@ -445,7 +444,7 @@ private:
void addObserver(NSKeyValueObservingOptions options);
#endif
NSObject *object = nullptr;
QObjCWeakPointer<NSObject> object;
NSString *keyPath = nullptr;
std::unique_ptr<Callback> callback;