summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorAndrei Golubev <andrei.golubev@qt.io>2021-01-15 09:42:19 +0100
committerAndrei Golubev <andrei.golubev@qt.io>2021-01-26 09:57:34 +0100
commit32d4e43f926c30bd1a8dd6e6744385d731908d06 (patch)
tree4b65f868f8df3a92852a7546516bd589ce226af6 /tests
parent2201f2425a19dfb594b8846cbc1a5622a8c7f5ca (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)
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 63d06497ce..9bd66c0835 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();
@@ -6783,6 +6784,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 { };