From 1ec44a0c3c1c389bb49107875cc803f5d8d053e2 Mon Sep 17 00:00:00 2001 From: bdm-oslandia Date: Mon, 2 Dec 2024 14:35:10 +0100 Subject: [PATCH] try shared_ptr + detach --- src/server/qgsfcgiserverresponse.cpp | 23 +++++++++++++---------- src/server/qgsfcgiserverresponse.h | 8 ++++---- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/server/qgsfcgiserverresponse.cpp b/src/server/qgsfcgiserverresponse.cpp index 8b099c47edf9..4bf1202a33c1 100644 --- a/src/server/qgsfcgiserverresponse.cpp +++ b/src/server/qgsfcgiserverresponse.cpp @@ -58,7 +58,6 @@ QgsSocketMonitoringThread::QgsSocketMonitoringThread( std::shared_ptr ptr ) +{ + mySelf = ptr; +} + void QgsSocketMonitoringThread::run( ) { if ( mIpcFd < 0 ) @@ -134,6 +134,7 @@ void QgsSocketMonitoringThread::run( ) QgsDebugMsgLevel( QStringLiteral( "FCGIServer: socket monitoring quits: no more socket." ), 2 ); } #endif + mySelf = nullptr; } @@ -149,16 +150,18 @@ QgsFcgiServerResponse::QgsFcgiServerResponse( QgsServerRequest::Method method ) setDefaultHeaders(); // This is not a unique_ptr because we want the response to not depend on the thread lifecycle. - mSocketMonitoringThread = new QgsSocketMonitoringThread( mFeedback ); - mSocketMonitoringThread->start(); + mSocketMonitoringThread = std::make_shared( mFeedback ); + mSocketMonitoringThread->setShared( mSocketMonitoringThread ); + std::thread mThread = std::thread( &QgsSocketMonitoringThread::run, mSocketMonitoringThread ); + mThread.detach(); } QgsFcgiServerResponse::~QgsFcgiServerResponse() { mFinished = true; - if ( mSocketMonitoringThread ) - // This will allow the thread to finish sleeping and exit its while loop in the background, without us needing to wait for it to finish. - mSocketMonitoringThread->setResponseFinished( mFinished ); + // if ( mSocketMonitoringThread ) + // This will allow the thread to finish sleeping and exit its while loop in the background, without us needing to wait for it to finish. + mSocketMonitoringThread->setResponseFinished( mFinished ); QgsDebugMsgLevel( QStringLiteral( "FCGIServer: response quits." ), 2 ); } diff --git a/src/server/qgsfcgiserverresponse.h b/src/server/qgsfcgiserverresponse.h index 5d569a027ecd..8badd5c51924 100644 --- a/src/server/qgsfcgiserverresponse.h +++ b/src/server/qgsfcgiserverresponse.h @@ -35,10 +35,8 @@ * \brief Thread used to monitor the fcgi socket * \since QGIS 3.36 */ -class QgsSocketMonitoringThread: public QThread +class QgsSocketMonitoringThread { - Q_OBJECT - public: /** @@ -50,11 +48,13 @@ class QgsSocketMonitoringThread: public QThread void run( ); void setResponseFinished( bool responseFinished ); + void setShared( std::shared_ptr ptr ); private: bool mIsResponseFinished = false; std::shared_ptr mFeedback; int mIpcFd = -1; + std::shared_ptr mySelf; }; /** @@ -123,7 +123,7 @@ class SERVER_EXPORT QgsFcgiServerResponse: public QgsServerResponse QgsServerRequest::Method mMethod; int mStatusCode = 0; - QPointer mSocketMonitoringThread; + std::shared_ptr mSocketMonitoringThread; std::shared_ptr mFeedback; };