summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2023-02-07 17:56:58 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2023-02-09 13:25:32 +0000
commit9d02a14d7ee2b5e78d0f4edcfa89067e275786ab (patch)
tree9d78d3b2a7d25e51cdf5117fb0b139cc312d67ea
parent823b31e54c1793f4d88c9d4a424a1e2e0ef2715c (diff)
QModbusTcpServer: fix memory leak
Noticed while improving Modbus Custom Command example. Before the patch the QTcpSocket::destroyed signal was connected to a lambda in context of the parent QTcpServer (the 'q' variable). When the application was closed without disconnecting the server in advance, the QTcpServer::close() was called in the server's destructor. The client sockets were disconnected, but by the time their destructors were called, the parent QTcpSocket was already in its desctuctor, so the connection was destroyed (because deleteChildren() is called after invalidating connections in QObject's d-tor). This patch simply changes the context of the lambda's connection to the socket object itself, so that the lamdba is correctly invoked from the socket's destructor. Change-Id: I04e196645db35c390a2bb255f1fadaac336fec25 Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io> (cherry picked from commit bfb62b60ad6873ad1912df0f08ce9cf7fd80447b) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/serialbus/qmodbustcpserver_p.h2
1 files changed, 1 insertions, 1 deletions
diff --git a/src/serialbus/qmodbustcpserver_p.h b/src/serialbus/qmodbustcpserver_p.h
index a845053..0f1cbfe 100644
--- a/src/serialbus/qmodbustcpserver_p.h
+++ b/src/serialbus/qmodbustcpserver_p.h
@@ -103,7 +103,7 @@ public:
auto buffer = new QByteArray();
- QObject::connect(socket, &QObject::destroyed, q, [buffer]() {
+ QObject::connect(socket, &QObject::destroyed, socket, [buffer]() {
// cleanup buffer
delete buffer;
});