Skip to content

Commit

Permalink
QTextStream: Always use direct connection inside QDeviceClosedNotifier
Browse files Browse the repository at this point in the history
Force direct connection when connecting aboutToClose() signal of device
to flushStream() slot of QDeviceClosedNotifier. This allows use of a
QTextStream from multiple threads when synchronization is handled by the
application.

Queued connections from aboutToClose() don't make much sense because the
device is closed immediately after emitting the signal. If a QTextStream
object is used by threads different from the one where it was created,
queued connection may result in attempting to flush the data after
the associated device is already closed, and accessing the QTextStream's
buffers from multiple threads.

Fixes: QTBUG-12055
Change-Id: If601d0f04f08b248b21ed1630b7dfd3546aee068
Reviewed-by: Oswald Buddenhagen <[email protected]>
Reviewed-by: Edward Welbourne <[email protected]>
Reviewed-by: Lars Knoll <[email protected]>
  • Loading branch information
eugmes authored and ossilator committed Jul 27, 2021
1 parent 0c8b987 commit 62b33b6
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/corelib/serialization/qtextstream_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ class QDeviceClosedNotifier : public QObject
inline void setupDevice(QTextStream *stream, QIODevice *device)
{
disconnect();
if (device)
connect(device, SIGNAL(aboutToClose()), this, SLOT(flushStream()));
if (device) {
// Force direct connection here so that QTextStream can be used
// from multiple threads when the application code is handling
// synchronization (see also QTBUG-12055).
connect(device, SIGNAL(aboutToClose()), this, SLOT(flushStream()),
Qt::DirectConnection);
}
this->stream = stream;
}

Expand Down

0 comments on commit 62b33b6

Please sign in to comment.