From 66e44f3448c8ef0b9fbf63f06211f8cdee55e0be Mon Sep 17 00:00:00 2001 From: RickyRister <42636155+RickyRister@users.noreply.github.com> Date: Sun, 22 Jun 2025 15:29:20 -0700 Subject: [PATCH] [PictureLoader] Fix worker leak (#6001) --- .../ui/picture_loader/picture_loader_worker_work.cpp | 12 ++++++------ .../ui/picture_loader/picture_loader_worker_work.h | 9 +++++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/cockatrice/src/client/ui/picture_loader/picture_loader_worker_work.cpp b/cockatrice/src/client/ui/picture_loader/picture_loader_worker_work.cpp index 3f508aee0..bcc5772f4 100644 --- a/cockatrice/src/client/ui/picture_loader/picture_loader_worker_work.cpp +++ b/cockatrice/src/client/ui/picture_loader/picture_loader_worker_work.cpp @@ -16,7 +16,7 @@ QStringList PictureLoaderWorkerWork::md5Blacklist = QStringList() << "db0c48db407a907c16ade38de048a441"; PictureLoaderWorkerWork::PictureLoaderWorkerWork(const PictureLoaderWorker *worker, const CardInfoPtr &toLoad) - : QThread(nullptr), cardToDownload(toLoad), picDownload(SettingsCache::instance().getPicDownload()) + : QObject(nullptr), cardToDownload(toLoad), picDownload(SettingsCache::instance().getPicDownload()) { // Hook up signals to the orchestrator connect(this, &PictureLoaderWorkerWork::requestImageDownload, worker, &PictureLoaderWorker::queueRequest, @@ -32,12 +32,12 @@ PictureLoaderWorkerWork::PictureLoaderWorkerWork(const PictureLoaderWorker *work connect(pictureLoaderThread, &QThread::started, this, &PictureLoaderWorkerWork::startNextPicDownload); - pictureLoaderThread->start(QThread::LowPriority); -} + // clean up worker once loading finishes + connect(this, &PictureLoaderWorkerWork::imageLoaded, this, &QObject::deleteLater); + connect(this, &QObject::destroyed, pictureLoaderThread, &QThread::quit); + connect(pictureLoaderThread, &QThread::finished, pictureLoaderThread, &QObject::deleteLater); -PictureLoaderWorkerWork::~PictureLoaderWorkerWork() -{ - pictureLoaderThread->deleteLater(); + pictureLoaderThread->start(QThread::LowPriority); } void PictureLoaderWorkerWork::startNextPicDownload() diff --git a/cockatrice/src/client/ui/picture_loader/picture_loader_worker_work.h b/cockatrice/src/client/ui/picture_loader/picture_loader_worker_work.h index 887479591..5db97938e 100644 --- a/cockatrice/src/client/ui/picture_loader/picture_loader_worker_work.h +++ b/cockatrice/src/client/ui/picture_loader/picture_loader_worker_work.h @@ -20,12 +20,12 @@ inline Q_LOGGING_CATEGORY(PictureLoaderWorkerWorkLog, "picture_loader.worker"); class PictureLoaderWorker; -class PictureLoaderWorkerWork : public QThread + +class PictureLoaderWorkerWork : public QObject { Q_OBJECT public: explicit PictureLoaderWorkerWork(const PictureLoaderWorker *worker, const CardInfoPtr &toLoad); - ~PictureLoaderWorkerWork() override; PictureToLoad cardToDownload; @@ -46,6 +46,11 @@ private slots: void picDownloadChanged(); signals: + /** + * Emitted when this worker has successfully loaded the image or has exhausted all attempts at loading the image. + * Failures are represented by an empty QImage. + * Note that this object will delete itself as this signal is emitted. + */ void imageLoaded(CardInfoPtr card, const QImage &image); void requestImageDownload(const QUrl &url, PictureLoaderWorkerWork *instance); };