Skip to content

Commit

Permalink
Fix crash when modifying list model in worker thread
Browse files Browse the repository at this point in the history
If we call get() on a model in a worker thread, we may end up creating a
ModelNodeMetaObject (aka cacheObject). Subsequent mutation of properties
may make us end up in emitDirectNotifies(). However since we can't have
bindings in there, we should shortcut/suppress the notify emission, which
we can do by checking ddata->context via qmlEngine(). The previous code
crashed when qmlEngine() return a null pointer but
QQmlEnginePrivate::get(const QQmlEngine *) would attempt to dereference
the parameter.

Started-by: Slava Monich<slava.monich@jolla.com>
Change-Id: I880619c686436c053692faafa5dba2c96c2ace96
Reviewed-by: Robin Burchell <robin.burchell@crimson.no>
Reviewed-by: Slava Monich <slava.monich@jolla.com>
  • Loading branch information
tronical authored and denexter committed Jun 5, 2018
1 parent 8d7a065 commit 13783ea
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/qml/types/qqmllistmodel.cpp
Expand Up @@ -1326,8 +1326,8 @@ void ModelNodeMetaObject::emitDirectNotifies(const int *changedRoles, int roleCo
QQmlData *ddata = QQmlData::get(object(), /*create*/false);
if (!ddata)
return;
QQmlEnginePrivate *ep = QQmlEnginePrivate::get(qmlEngine(m_model));
if (!ep)
// There's nothing to emit if we're a list model in a worker thread.
if (!qmlEngine(m_model))
return;
for (int i = 0; i < roleCount; ++i) {
const int changedRole = changedRoles[i];
Expand Down
Expand Up @@ -204,6 +204,7 @@ void tst_qqmllistmodelworkerscript::dynamic_data()
QTest::newRow("get4") << "{append({'foo':123});get(0).foo}" << 123 << "" << dr;
QTest::newRow("get-modify1") << "{append({'foo':123,'bar':456});get(0).foo = 333;get(0).foo}" << 333 << "" << dr;
QTest::newRow("get-modify2") << "{append({'z':1});append({'foo':123,'bar':456});get(1).bar = 999;get(1).bar}" << 999 << "" << dr;
QTest::newRow("get-set") << "{append({'foo':123});get(0).foo;setProperty(0, 'foo', 999);get(0).foo}" << 999 << "" << dr;

QTest::newRow("append1") << "{append({'foo':123});count}" << 1 << "" << dr;
QTest::newRow("append2") << "{append({'foo':123,'bar':456});count}" << 1 << "" << dr;
Expand Down

0 comments on commit 13783ea

Please sign in to comment.