diff --git a/libraries/qml/src/qml/OffscreenSurface.cpp b/libraries/qml/src/qml/OffscreenSurface.cpp index 91532534e3..abab5391e2 100644 --- a/libraries/qml/src/qml/OffscreenSurface.cpp +++ b/libraries/qml/src/qml/OffscreenSurface.cpp @@ -389,6 +389,10 @@ void OffscreenSurface::finishQmlLoad(QQmlComponent* qmlComponent, } // Allow child windows to be destroyed from JS QQmlEngine::setObjectOwnership(newObject, QQmlEngine::JavaScriptOwnership); + + // add object to the manual deletion list + _sharedObject->addToDeletionList(newObject); + newObject->setParent(parent); newItem->setParentItem(parent); } else { diff --git a/libraries/qml/src/qml/impl/SharedObject.cpp b/libraries/qml/src/qml/impl/SharedObject.cpp index 259defdb48..5bcca0821f 100644 --- a/libraries/qml/src/qml/impl/SharedObject.cpp +++ b/libraries/qml/src/qml/impl/SharedObject.cpp @@ -15,6 +15,7 @@ #include #include +#include #include #include @@ -96,6 +97,15 @@ SharedObject::~SharedObject() { } #endif + // already deleted objects will be reset to null by QPointer so it should be safe just iterate here + for (auto qmlObject : _deletionList) { + if (qmlObject) { + // manually delete not-deleted-yet qml items + QQmlEngine::setObjectOwnership(qmlObject, QQmlEngine::CppOwnership); + delete qmlObject; + } + } + if (_rootItem) { delete _rootItem; _rootItem = nullptr; @@ -412,6 +422,11 @@ bool SharedObject::fetchTexture(TextureAndFence& textureAndFence) { return true; } +void hifi::qml::impl::SharedObject::addToDeletionList(QObject * object) +{ + _deletionList.append(QPointer(object)); +} + void SharedObject::setProxyWindow(QWindow* window) { #ifndef DISABLE_QML _proxyWindow = window; diff --git a/libraries/qml/src/qml/impl/SharedObject.h b/libraries/qml/src/qml/impl/SharedObject.h index 002679c44d..ce9fcd46d2 100644 --- a/libraries/qml/src/qml/impl/SharedObject.h +++ b/libraries/qml/src/qml/impl/SharedObject.h @@ -66,7 +66,7 @@ public: void resume(); bool isPaused() const; bool fetchTexture(TextureAndFence& textureAndFence); - + void addToDeletionList(QObject* object); private: bool event(QEvent* e) override; @@ -91,6 +91,8 @@ private: void onAboutToQuit(); void updateTextureAndFence(const TextureAndFence& newTextureAndFence); + QList> _deletionList; + // Texture management TextureAndFence _latestTextureAndFence{ 0, 0 }; QQuickItem* _item{ nullptr };