From 38cd1fc491f93da32305fc9e38fd10802fd6746f Mon Sep 17 00:00:00 2001 From: David Redondo Date: Tue, 21 Feb 2023 10:53:38 +0100 Subject: [PATCH] scripting: Mark QObjects returned from invokables as having C++ ownership Otherwise the script engine may delete them if they have no parent and there are no more references to them. --- src/scripting/workspace_wrapper.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/scripting/workspace_wrapper.cpp b/src/scripting/workspace_wrapper.cpp index 1a46bb894e..b892a86e5b 100644 --- a/src/scripting/workspace_wrapper.cpp +++ b/src/scripting/workspace_wrapper.cpp @@ -21,6 +21,8 @@ #include "activities.h" #endif +#include + namespace KWin { @@ -281,7 +283,9 @@ void WorkspaceWrapper::hideOutline() Window *WorkspaceWrapper::getClient(qulonglong windowId) { - return Workspace::self()->findClient(Predicate::WindowMatch, windowId); + auto window = Workspace::self()->findClient(Predicate::WindowMatch, windowId); + QQmlEngine::setObjectOwnership(window, QQmlEngine::CppOwnership); + return window; } QSize WorkspaceWrapper::desktopGridSize() const @@ -321,7 +325,9 @@ QList WorkspaceWrapper::screens() const Output *WorkspaceWrapper::screenAt(const QPointF &pos) const { - return workspace()->outputAt(pos); + auto output = workspace()->outputAt(pos); + QQmlEngine::setObjectOwnership(output, QQmlEngine::CppOwnership); + return output; } QRect WorkspaceWrapper::virtualScreenGeometry() const @@ -343,14 +349,18 @@ KWin::TileManager *WorkspaceWrapper::tilingForScreen(const QString &screenName) { Output *output = kwinApp()->outputBackend()->findOutput(screenName); if (output) { - return workspace()->tileManager(output); + auto tileManager = workspace()->tileManager(output); + QQmlEngine::setObjectOwnership(tileManager, QQmlEngine::CppOwnership); + return tileManager; } return nullptr; } KWin::TileManager *WorkspaceWrapper::tilingForScreen(Output *output) const { - return workspace()->tileManager(output); + auto tileManager = workspace()->tileManager(output); + QQmlEngine::setObjectOwnership(tileManager, QQmlEngine::CppOwnership); + return tileManager; } QtScriptWorkspaceWrapper::QtScriptWorkspaceWrapper(QObject *parent)