From c31026c9ca7ff0c745dba577f9ac2c14d1ef68c5 Mon Sep 17 00:00:00 2001 From: Matthew Vogt Date: Fri, 27 Apr 2012 16:12:09 +1000 Subject: Emit Component.onDestruction before context is invalidated When a component no longer has any live references, emit the destruction signal immediately so that handlers are run before the associated V8 resources are invalidated. Also, when the root context of the engine is destroyed, emit the destruction signal before destroying any resources needed to process the resulting binding invocations. Change-Id: I722dd6e4b60c499b533fc45e33b61e95bca6187f Reviewed-by: Michael Brasser --- src/qml/qml/qqmlcontext.cpp | 54 +++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 22 deletions(-) (limited to 'src/qml/qml/qqmlcontext.cpp') diff --git a/src/qml/qml/qqmlcontext.cpp b/src/qml/qml/qqmlcontext.cpp index cbb8430f40..7e15993a65 100644 --- a/src/qml/qml/qqmlcontext.cpp +++ b/src/qml/qml/qqmlcontext.cpp @@ -513,7 +513,8 @@ QObject *QQmlContextPrivate::context_at(QQmlListProperty *prop, int ind QQmlContextData::QQmlContextData() : parent(0), engine(0), isInternal(false), ownedByParent(false), isJSContext(false), - isPragmaLibraryContext(false), unresolvedNames(false), publicContext(0), activeVMEData(0), + isPragmaLibraryContext(false), unresolvedNames(false), hasEmittedDestruction(false), + publicContext(0), activeVMEData(0), propertyNames(0), contextObject(0), imports(0), childContexts(0), nextChild(0), prevChild(0), expressions(0), contextObjects(0), contextGuards(0), idValues(0), idValueCount(0), linkedContext(0), componentAttached(0), v4bindings(0), v8bindings(0) @@ -522,25 +523,45 @@ QQmlContextData::QQmlContextData() QQmlContextData::QQmlContextData(QQmlContext *ctxt) : parent(0), engine(0), isInternal(false), ownedByParent(false), isJSContext(false), - isPragmaLibraryContext(false), unresolvedNames(false), publicContext(ctxt), activeVMEData(0), + isPragmaLibraryContext(false), unresolvedNames(false), hasEmittedDestruction(false), + publicContext(ctxt), activeVMEData(0), propertyNames(0), contextObject(0), imports(0), childContexts(0), nextChild(0), prevChild(0), expressions(0), contextObjects(0), contextGuards(0), idValues(0), idValueCount(0), linkedContext(0), componentAttached(0), v4bindings(0), v8bindings(0) { } -void QQmlContextData::invalidate() +void QQmlContextData::emitDestruction() { - while (componentAttached) { - QQmlComponentAttached *a = componentAttached; - componentAttached = a->next; - if (componentAttached) componentAttached->prev = &componentAttached; + if (!hasEmittedDestruction) { + hasEmittedDestruction = true; + + // Emit the destruction signal - must be emitted before invalidate so that the + // context is still valid if bindings or resultant expression evaluation requires it + if (engine) { + while (componentAttached) { + QQmlComponentAttached *a = componentAttached; + componentAttached = a->next; + if (componentAttached) componentAttached->prev = &componentAttached; - a->next = 0; - a->prev = 0; + a->next = 0; + a->prev = 0; + + emit a->destruction(); + } - emit a->destruction(); + QQmlContextData * child = childContexts; + while (child) { + child->emitDestruction(); + child = child->nextChild; + } + } } +} + +void QQmlContextData::invalidate() +{ + emitDestruction(); while (childContexts) { if (childContexts->ownedByParent) { @@ -563,18 +584,7 @@ void QQmlContextData::invalidate() void QQmlContextData::clearContext() { - if (engine) { - while (componentAttached) { - QQmlComponentAttached *a = componentAttached; - componentAttached = a->next; - if (componentAttached) componentAttached->prev = &componentAttached; - - a->next = 0; - a->prev = 0; - - emit a->destruction(); - } - } + emitDestruction(); QQmlAbstractExpression *expression = expressions; while (expression) { -- cgit v1.2.3