From dd517d02174f43af4e0bcdd4039ce2e8e0251865 Mon Sep 17 00:00:00 2001 From: Hannes Matuschek Date: Thu, 20 Jan 2022 09:16:48 +0100 Subject: [PATCH] Fixed stupid cast mistake in AbstractConfigObjectList. Addresses #194. --- lib/configobject.cc | 8 ++++++-- lib/configobject.hh | 3 ++- lib/configreference.cc | 5 ++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/configobject.cc b/lib/configobject.cc index ec029fd8..b6d98863 100644 --- a/lib/configobject.cc +++ b/lib/configobject.cc @@ -985,9 +985,13 @@ AbstractConfigObjectList::onElementModified(ConfigItem *obj) { void AbstractConfigObjectList::onElementDeleted(QObject *obj) { - int idx = indexOf(qobject_cast(obj)); - if (0 <= idx) + // Use reinterpret cast here as the obj may already be destroyed and this all RTTI freed. + // We just use the pointer address to remove the element here. + int idx = indexOf(reinterpret_cast(obj)); + if (0 <= idx) { + _items.remove(idx); emit elementRemoved(idx); + } } diff --git a/lib/configobject.hh b/lib/configobject.hh index 9ce59272..984acedf 100644 --- a/lib/configobject.hh +++ b/lib/configobject.hh @@ -165,7 +165,8 @@ protected: virtual bool populate(YAML::Node &node, const Context &context); signals: - /** Gets emitted once the config object is modified. */ + /** Gets emitted once the config object is modified. + * The instance passed is the modified item, this event is passed up the config tree. */ void modified(ConfigItem *obj); /** Gets emitted before clearing the item. */ void beginClear(); diff --git a/lib/configreference.cc b/lib/configreference.cc index ebcddaef..4e8b58ee 100644 --- a/lib/configreference.cc +++ b/lib/configreference.cc @@ -81,7 +81,10 @@ ConfigObjectReference::allow(const QMetaObject *elementType) { void ConfigObjectReference::onReferenceDeleted(QObject *obj) { - Q_UNUSED(obj) + // Check if destroyed obj is referenced one. + if (_object != reinterpret_cast(obj)) + return; + // If it is _object = nullptr; emit modified(); }