diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2023-02-07 17:56:58 +0100 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2023-02-09 10:11:49 +0100 |
commit | bfb62b60ad6873ad1912df0f08ce9cf7fd80447b (patch) | |
tree | 322c95af1902607feeaa8ce7a0f7e923614669da | |
parent | 0ac11c92929449cd77c9e867be329bf56eba8d94 (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.
Pick-to: 6.5 6.4 6.2
Change-Id: I04e196645db35c390a2bb255f1fadaac336fec25
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
-rw-r--r-- | src/serialbus/qmodbustcpserver_p.h | 2 |
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; }); |