From a99b9aedf16145d9d11cc78fd97c7159c7989226 Mon Sep 17 00:00:00 2001 From: Mate Barany Date: Tue, 25 Jun 2024 15:12:51 +0200 Subject: [PATCH] Remove ProtocolHandlerDeleter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It has only a one time use and there is a nicer way to do that. Found by an Axivion scan. Task-number: QTBUG-125026 Change-Id: I9de8263fe1ea55608a8129f990b490e2f86e9479 Reviewed-by: MÃ¥rten Nordheim --- .../access/qhttpnetworkconnectionchannel.cpp | 32 ++++--------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/src/network/access/qhttpnetworkconnectionchannel.cpp b/src/network/access/qhttpnetworkconnectionchannel.cpp index 8688e4b8d71..336256f7714 100644 --- a/src/network/access/qhttpnetworkconnectionchannel.cpp +++ b/src/network/access/qhttpnetworkconnectionchannel.cpp @@ -27,20 +27,6 @@ QT_BEGIN_NAMESPACE -namespace -{ - -class ProtocolHandlerDeleter : public QObject -{ -public: - explicit ProtocolHandlerDeleter(QAbstractProtocolHandler *h) : handler(h) {} - ~ProtocolHandlerDeleter() { delete handler; } -private: - QAbstractProtocolHandler *handler = nullptr; -}; - -} - // TODO: Put channel specific stuff here so it does not pollute qhttpnetworkconnection.cpp // Because in-flight when sending a request, the server might close our connection (because the persistent HTTP @@ -475,18 +461,12 @@ void QHttpNetworkConnectionChannel::allDone() // As allDone() gets called from the protocol handler, it's not yet // safe to delete it. There is no 'deleteLater', since - // QAbstractProtocolHandler is not a QObject. Instead we do this - // trick with ProtocolHandlerDeleter, a QObject-derived class. - // These dances below just make it somewhat exception-safe. - // 1. Create a new owner: - QAbstractProtocolHandler *oldHandler = protocolHandler.get(); - auto deleter = std::make_unique(oldHandler); - // 2. Retire the old one: - Q_UNUSED(protocolHandler.release()); - // 3. Call 'deleteLater': - deleter->deleteLater(); - // 3. Give up the ownerthip: - Q_UNUSED(deleter.release()); + // QAbstractProtocolHandler is not a QObject. Instead delete it in + // a queued emission. + + QMetaObject::invokeMethod(this, [oldHandler = std::move(protocolHandler)]() mutable { + oldHandler.reset(); + }, Qt::QueuedConnection); connection->fillHttp2Queue(); protocolHandler.reset(new QHttp2ProtocolHandler(this));