diff options
| author | Ulf Hermann <ulf.hermann@qt.io> | 2025-08-07 16:42:22 +0200 |
|---|---|---|
| committer | Ulf Hermann <ulf.hermann@qt.io> | 2025-09-02 09:15:10 +0200 |
| commit | b1c48db754c7019e0472d0baf6d71d2bd93a205b (patch) | |
| tree | 59d174c30931ab8a95bc513d840553e182cc4f07 /tests/auto/qml/qmlcppcodegen/data | |
| parent | 2d016a2653c59f10a57dc1903b817f71d16d0622 (diff) | |
QmlCompiler: Ensure QObjects returned to AOT-compiled code are wrapped
If a QObject is returned from a method call, the QML engine takes
ownership of it and it needs to be deleted by the garbage collector. Our
generated C++ code so far did not actually take ownership of the object
and thereby caused it to leak.
Pick-to: 6.10 6.9 6.8
Fixes: QTBUG-138919
Change-Id: I7bd57b3612bf4b98937756e8a7a7c03aff1c9b32
Reviewed-by: Olivier De Cannière <olivier.decanniere@qt.io>
Diffstat (limited to 'tests/auto/qml/qmlcppcodegen/data')
| -rw-r--r-- | tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | tests/auto/qml/qmlcppcodegen/data/callFactory.qml | 10 | ||||
| -rw-r--r-- | tests/auto/qml/qmlcppcodegen/data/cppobj.h | 46 |
3 files changed, 58 insertions, 0 deletions
diff --git a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt index 4b5dd31c0a..c0da4370fc 100644 --- a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt +++ b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt @@ -10,6 +10,7 @@ set(cpp_sources collector.h convertQJSPrimitiveValueToIntegral.h cppbaseclass.h + cppobj.h detachedreferences.h druggeljug.h dummyobjekt.h @@ -116,6 +117,7 @@ set(qml_files brokenAs.qml boundComponents.qml callContextPropertyLookupResult.qml + callFactory.qml callObjectLookupOnNull.qml callWithSpread.qml childobject.qml diff --git a/tests/auto/qml/qmlcppcodegen/data/callFactory.qml b/tests/auto/qml/qmlcppcodegen/data/callFactory.qml new file mode 100644 index 0000000000..1b5d9eaec8 --- /dev/null +++ b/tests/auto/qml/qmlcppcodegen/data/callFactory.qml @@ -0,0 +1,10 @@ +pragma Strict +import QtQml +import TestTypes + +QtObject { + Component.onCompleted: { + // Call the factory function and then forget the object + const orphanedObj = CppBridge.singleObj(0) + } +} diff --git a/tests/auto/qml/qmlcppcodegen/data/cppobj.h b/tests/auto/qml/qmlcppcodegen/data/cppobj.h new file mode 100644 index 0000000000..3b64c21e74 --- /dev/null +++ b/tests/auto/qml/qmlcppcodegen/data/cppobj.h @@ -0,0 +1,46 @@ +// Copyright (C) 2025 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#ifndef CPPOBJ_H +#define CPPOBJ_H + +#include <QtCore/qobject.h> +#include <QtCore/qdebug.h> +#include <QtQmlIntegration/qqmlintegration.h> + +class CppObj : public QObject +{ + Q_OBJECT + QML_ELEMENT + Q_PROPERTY(int identifier MEMBER m_identifier CONSTANT) + +public: + explicit CppObj(int identifier = -1, QObject *parent = nullptr) + : QObject(parent) + , m_identifier(identifier) + { + qDebug() << "Object created:" << m_identifier; + } + + ~CppObj() { qDebug() << "Object destroyed:" << m_identifier; } + +private: + int m_identifier = -1; +}; + +class CppBridge : public QObject +{ + Q_OBJECT + QML_ELEMENT + QML_SINGLETON + +public: + explicit CppBridge(QObject *parent = nullptr) : QObject{parent} {} + + Q_INVOKABLE CppObj *singleObj(int identifier) const + { + return new CppObj(identifier); + } +}; + +#endif // CPPOBJ_H |
