summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorAndrei Golubev <andrei.golubev@qt.io>2021-01-15 09:42:19 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-01-21 09:06:54 +0000
commitf83f4badce46b5572fa4c85ba2c3420a19d28450 (patch)
tree383d2a3c5afdf838a4f6ca0dc9d7e1f2b9344ecf /tests
parent563f8bc744f79a093cfda5467c9ee5a6d0081cf7 (diff)
Add new special QObjectPrivate::{connect, disconnect} for QML
Original QML-specific connection mechanism ignores the receiver argument and uses sender as receiver. This causes uncontrollable memory growth in certain cases as connections on receiver persist even after receiver is destroyed New connect() with receiver parameter uses underlying API correctly, disconnect is provided for the symmetry (not sure it's really needed) Task-number: QTBUG-86368 Change-Id: I4580d75b617cb2c4dfb971a4dfb8e943e325572b Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> (cherry picked from commit 6f520abdab7120789800208dde837b3836f762cc) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/kernel/qobject/tst_qobject.cpp28
1 files changed, 28 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp
index 5cf1f0e50f..68d90987e1 100644
--- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp
+++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp
@@ -149,6 +149,7 @@ private slots:
void connectBase();
void connectWarnings();
void qmlConnect();
+ void qmlConnectToQObjectReceiver();
void exceptions();
void noDeclarativeParentChangedOnDestruction();
void deleteLaterInAboutToBlockHandler();
@@ -6787,6 +6788,33 @@ void tst_QObject::qmlConnect()
#endif
}
+void tst_QObject::qmlConnectToQObjectReceiver()
+{
+#ifdef QT_BUILD_INTERNAL
+ SenderObject sender;
+ QScopedPointer<QObject> receiver(new QObject);
+ QmlReceiver *slotObject = new QmlReceiver;
+ slotObject->magic = slotObject;
+ slotObject->ref(); // extra ref so that slot object is not implicitly deleted
+
+ QVERIFY(QObjectPrivate::connect(&sender, sender.metaObject()->indexOfSignal("signal1()"),
+ receiver.get(), slotObject, Qt::AutoConnection));
+
+ QCOMPARE(slotObject->callCount, 0);
+ sender.emitSignal1();
+ QCOMPARE(slotObject->callCount, 1);
+
+ receiver.reset(); // this should disconnect the slotObject
+
+ sender.emitSignal1();
+ QCOMPARE(slotObject->callCount, 1);
+
+ slotObject->destroyIfLastRef();
+#else
+ QSKIP("Needs QT_BUILD_INTERNAL");
+#endif
+}
+
#ifndef QT_NO_EXCEPTIONS
class ObjectException : public std::exception { };