From b5d5be9db8ea0932b728fb9b90f61212c7cb6777 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Thu, 18 Mar 2010 15:27:05 +1000 Subject: Fix crash when calling createObject on a component with errors. --- src/declarative/qml/qdeclarativecomponent.cpp | 2 ++ .../qdeclarativeecmascript/data/dynamicCreation.qml | 6 ++++++ .../qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp | 14 ++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/src/declarative/qml/qdeclarativecomponent.cpp b/src/declarative/qml/qdeclarativecomponent.cpp index a280d7e..8922751 100644 --- a/src/declarative/qml/qdeclarativecomponent.cpp +++ b/src/declarative/qml/qdeclarativecomponent.cpp @@ -511,6 +511,8 @@ QScriptValue QDeclarativeComponent::createObject() return QScriptValue(); } QObject* ret = create(ctxt); + if (!ret) + return QScriptValue(); QDeclarativeEnginePrivate *priv = QDeclarativeEnginePrivate::get(d->engine); QDeclarativeDeclarativeData::get(ret, true)->setImplicitDestructible(); return priv->objectClass->newQObject(ret, QMetaType::QObjectStar); diff --git a/tests/auto/declarative/qdeclarativeecmascript/data/dynamicCreation.qml b/tests/auto/declarative/qdeclarativeecmascript/data/dynamicCreation.qml index ed5e571..2fef03a 100644 --- a/tests/auto/declarative/qdeclarativeecmascript/data/dynamicCreation.qml +++ b/tests/auto/declarative/qdeclarativeecmascript/data/dynamicCreation.qml @@ -18,4 +18,10 @@ MyQmlObject{ { obj.objectProperty = createQmlObject('TypeForDynamicCreation{}', obj); } + + function dontCrash() + { + var component = createComponent('file-doesnt-exist.qml'); + obj.objectProperty = component.createObject(); + } } diff --git a/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp b/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp index caefdbf..87d73a0 100644 --- a/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp +++ b/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp @@ -130,6 +130,7 @@ private slots: void qlistqobjectMethods(); void bug1(); + void dynamicCreationCrash(); void callQtInvokables(); private: @@ -1227,6 +1228,19 @@ void tst_qdeclarativeecmascript::bug1() delete object; } +// Don't crash in createObject when the component has errors. +void tst_qdeclarativeecmascript::dynamicCreationCrash() +{ + QDeclarativeComponent component(&engine, TEST_FILE("dynamicCreation.qml")); + MyQmlObject *object = qobject_cast(component.create()); + QVERIFY(object != 0); + + QTest::ignoreMessage(QtWarningMsg, "QDeclarativeComponent: Component is not ready"); + QMetaObject::invokeMethod(object, "dontCrash"); + QObject *created = object->objectProperty(); + QVERIFY(created == 0); +} + void tst_qdeclarativeecmascript::callQtInvokables() { MyInvokableObject o; -- cgit v0.12