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/cppobj.h | |
| 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/cppobj.h')
| -rw-r--r-- | tests/auto/qml/qmlcppcodegen/data/cppobj.h | 46 |
1 files changed, 46 insertions, 0 deletions
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 |
