From 46a20b90243149b0ecd1d44cab964e785648d214 Mon Sep 17 00:00:00 2001 From: Joerg Bornemann Date: Wed, 13 Feb 2019 10:09:54 +0100 Subject: [PATCH] QProcess: Fix crash when calling closeWriteChannel on Windows We must deleteLater the pipe writer in closeChannel, because if you call closeWriteChannel() from a slot that is connected to a signal emitted from QWindowsPipeWriter, we'd operate on a deleted object. For consistency, we're calling QWindowsPipeWriter::stop before deleteLater and deduplicate the code. Fixes: QTBUG-73778 Change-Id: I61d3dedf57e9fd02517a108d13ffc85e006330f6 Reviewed-by: Christian Kandeler --- src/corelib/io/qprocess_win.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/corelib/io/qprocess_win.cpp b/src/corelib/io/qprocess_win.cpp index cb4b2f9f91..3ba86063e3 100644 --- a/src/corelib/io/qprocess_win.cpp +++ b/src/corelib/io/qprocess_win.cpp @@ -362,16 +362,22 @@ void QProcessPrivate::destroyPipe(Q_PIPE pipe[2]) } } +template +void deleteWorker(T *&worker) +{ + if (!worker) + return; + worker->stop(); + worker->deleteLater(); + worker = nullptr; +} + void QProcessPrivate::closeChannel(Channel *channel) { - if (channel == &stdinChannel) { - delete stdinChannel.writer; - stdinChannel.writer = 0; - } else if (channel->reader) { - channel->reader->stop(); - channel->reader->deleteLater(); - channel->reader = 0; - } + if (channel == &stdinChannel) + deleteWorker(channel->writer); + else + deleteWorker(channel->reader); destroyPipe(channel->pipe); }