Add a new flag inCreation to QQmlData. Set it to true when a top-level object begins creation,
and back to false when initial creation is finished and the object has been marked indestructible.
In the GC callback function, if inCreation is true, skip the GC and make a weak reference for next GC loop.
Change-Id: I4ec82864c52f6be0c3e6ef892474dd77d835e152
Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
//if JS ownership is needed this needs to be explicitly undone (like in component.createObject())
ddata->indestructible = true;
ddata->explicitIndestructibleSet = true;
+ ddata->inCreation = false;
}
if (enginePriv->isDebugging && rv) {
public:
QQmlData()
: ownMemory(true), ownContext(false), indestructible(true), explicitIndestructibleSet(false),
- hasTaintedV8Object(false), isQueuedForDeletion(false), notifyList(0), context(0), outerContext(0),
+ hasTaintedV8Object(false), isQueuedForDeletion(false), inCreation(false), notifyList(0), context(0), outerContext(0),
bindings(0), signalHandlers(0), nextContextObject(0), prevContextObject(0), bindingBitsSize(0), bindingBits(0),
lineNumber(0), columnNumber(0), deferredComponent(0), deferredIdx(0), v8objectid(0),
propertyCache(0), guards(0), extendedData(0) {
quint32 explicitIndestructibleSet:1;
quint32 hasTaintedV8Object:1;
quint32 isQueuedForDeletion:1;
- quint32 dummy:26;
+ /*
+ * inCreation should be true only when creating top level CPP and QML objects,
+ * v8 GC will check this flag, only deletes the objects when inCreation is false.
+ */
+ quint32 inCreation:1;
+ quint32 dummy:25;
struct NotifyList {
quint64 connectionMask;
//see QQmlComponent::beginCreate for explanation of indestructible
ddata->indestructible = true;
ddata->explicitIndestructibleSet = true;
+ ddata->inCreation = false;
q->setInitialState(result);
}
QQmlData *ddata = QQmlData::get(o);
Q_ASSERT(ddata);
+ if (states.count() == 1)
+ ddata->inCreation = true;
+
if (instr.isRoot) {
if (ddata->context) {
Q_ASSERT(ddata->context != CTXT);
ddata->ownMemory = false;
QObjectPrivate::get(o)->declarativeData = ddata;
+ if (states.count() == 1)
+ ddata->inCreation = true;
+
if (type.typePropertyCache && !ddata->propertyCache) {
ddata->propertyCache = type.typePropertyCache;
ddata->propertyCache->addref();
if (object) {
QQmlData *ddata = QQmlData::get(object, false);
if (ddata) {
+ if (ddata->inCreation) {
+ ddata->v8object.MakeWeak(0, WeakQObjectReferenceCallback);
+ return;
+ }
+
ddata->v8object.Clear();
if (!object->parent() && !ddata->indestructible) {
ddata->isQueuedForDeletion = true;
static v8::Handle<v8::Value> Destroy(QV8Engine *, QObject *object, int argCount, v8::Handle<v8::Object> args)
{
QQmlData *ddata = QQmlData::get(object, false);
- if (!ddata || ddata->indestructible) {
+ if (!ddata || ddata->indestructible || ddata->inCreation) {
const char *error = "Invalid attempt to destroy() an indestructible object";
v8::ThrowException(v8::Exception::Error(v8::String::New(error)));
return v8::Undefined();
--- /dev/null
+import QtQuick 2.0
+
+Rectangle {
+ id: root
+ color:"red"
+ property bool rootIndestructible:false
+ property bool childDestructible:child === null
+ onColorChanged: {
+ try {
+ root.destroy();
+ gc();
+ } catch(e) {
+ rootIndestructible = true;
+ }
+ }
+
+ QtObject {
+ id:child
+ }
+ Component.onCompleted: child.destroy();
+}
\ No newline at end of file
--- /dev/null
+import QtQuick 2.0
+
+RootObject {
+ color:"black"
+}
\ No newline at end of file
void withStatement();
void tryStatement();
void replaceBinding();
-
+ void deleteRootObjectInCreation();
private:
static void propertyVarWeakRefCallback(v8::Persistent<v8::Value> object, void* parameter);
QQmlEngine engine;
delete obj;
}
+void tst_qqmlecmascript::deleteRootObjectInCreation()
+{
+ QQmlEngine engine;
+ QQmlComponent c(&engine, testFileUrl("deleteRootObjectInCreation.qml"));
+ QObject *obj = c.create();
+ QVERIFY(obj != 0);
+ QVERIFY(obj->property("rootIndestructible").toBool());
+ QVERIFY(!obj->property("childDestructible").toBool());
+ QTest::qWait(1);
+ QVERIFY(obj->property("childDestructible").toBool());
+ delete obj;
+}
+
QTEST_MAIN(tst_qqmlecmascript)
#include "tst_qqmlecmascript.moc"