aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlmodels/qqmladaptormodel.cpp
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2022-01-31 14:14:26 +0100
committerFabian Kosmale <fabian.kosmale@qt.io>2022-02-03 12:59:43 +0000
commitce8db0672557cef7942df061f52ccdf65fa250f6 (patch)
tree4a7b9d231151c736532e09bdc3a45b35cb474403 /src/qmlmodels/qqmladaptormodel.cpp
parentb81a4a6e76970a330e255c57ed68e14694b25d41 (diff)
QQmlAdaptorModel: Do not use reparenting for lifetime managemment
In QQmlAdaptorModel, we were using QQmlStrongJSQObjectReference to ensure that a passed in model lives long enough. However, QQmlAdaptorModel uses reparenting to keep objects alive. This is not safe, as we can use QML singletons as models. Reparenting singletons messes with the engine's lifetime handling once their new parent gets deleted: The object will be marked as queuedForDeletion by QQmlData::markAsDeleted; consequently wasDeleted returns true for the object, and any ScopedObject or ObjectWrapper will return nullptr when we try to retrieve their underlying QObject. The actual object probaly does not get deleted, as it is not placed in the QML heap. Consequently the gc will ignore it. This leads to a crash when the singleton is accessed in a different place: We see that the object is non-null, create a ScopedObject for it, and then try to later access the ScopedObject's underlying object (assuming that it must be non-null, because we already checked for the actual object being non-null). However, due to the reasons outlined above, we actually receive a null pointer, and thus encounter a crash. To avoid he issue, we change the lifetime management strategy: Instead of using the parent to keep the object alive, we now use a QV4::PersistentValue. Fixes: QTBUG-100260 Change-Id: I266e6ef94c4f079de3da2742d6fb8d61df5a64ce Reviewed-by: Andrei Golubev <andrei.golubev@qt.io> Reviewed-by: Ulf Hermann <ulf.hermann@qt.io> (cherry picked from commit 6901eacff40a7d8781e20fb5bcfd28d7526b589b)
Diffstat (limited to 'src/qmlmodels/qqmladaptormodel.cpp')
-rw-r--r--src/qmlmodels/qqmladaptormodel.cpp18
1 files changed, 12 insertions, 6 deletions
diff --git a/src/qmlmodels/qqmladaptormodel.cpp b/src/qmlmodels/qqmladaptormodel.cpp
index dc3ea8019e..fe0ab6bcbc 100644
--- a/src/qmlmodels/qqmladaptormodel.cpp
+++ b/src/qmlmodels/qqmladaptormodel.cpp
@@ -962,30 +962,36 @@ QQmlAdaptorModel::~QQmlAdaptorModel()
accessors->cleanup(*this);
}
-void QQmlAdaptorModel::setModel(const QVariant &variant, QObject *parent)
+void QQmlAdaptorModel::setModel(const QVariant &variant, QObject *)
{
accessors->cleanup(*this);
list.setList(variant);
+ modelStrongReference.clear();
if (QObject *object = qvariant_cast<QObject *>(list.list())) {
- setObject(object, parent);
+ if (QQmlData *ddata = QQmlData::get(object))
+ modelStrongReference = ddata->jsWrapper;
+ setObject(object);
if (qobject_cast<QAbstractItemModel *>(object))
accessors = new VDMAbstractItemModelDataType(this);
else
accessors = new VDMObjectDelegateDataType;
} else if (list.type() == QQmlListAccessor::ListProperty) {
- setObject(static_cast<const QQmlListReference *>(variant.constData())->object(), parent);
+ auto object = static_cast<const QQmlListReference *>(variant.constData())->object();
+ if (QQmlData *ddata = QQmlData::get(object))
+ modelStrongReference = ddata->jsWrapper;
+ setObject(object);
accessors = new VDMObjectDelegateDataType;
} else if (list.type() == QQmlListAccessor::ObjectList) {
- setObject(nullptr, parent);
+ setObject(nullptr);
accessors = new VDMObjectDelegateDataType;
} else if (list.type() != QQmlListAccessor::Invalid
&& list.type() != QQmlListAccessor::Instance) { // Null QObject
- setObject(nullptr, parent);
+ setObject(nullptr);
accessors = new VDMListDelegateDataType;
} else {
- setObject(nullptr, parent);
+ setObject(nullptr);
accessors = &qt_vdm_null_accessors;
}
}