From 2d73d5f25a1a364a2115fb1d3262377f7fdf99cd Mon Sep 17 00:00:00 2001 From: Valeriy Malov Date: Wed, 5 Sep 2018 20:43:51 +0300 Subject: Fix memory corruption in ScanFolder plugin Summary: Reasoning behind this patch is kinda dubious because I don't fully understand what happens, but it shouldn't make the matters worse either Seems like deletion of KDirWatch objects from scan_folder in ~ScanThread(), after said QThread has been stopped, causes memory corruption. Preemptively deleting scan_folder contents before we stop the thread resolves this. also mark booleans shared across main thread and KDirWatch callbacks as atomic Test Plan: Without this patch unloading ScanFolder plugin sometimes crashes ktorrent, including unloading on exit Doesn't happen anymore with this patch Reviewers: stikonas Reviewed By: stikonas Differential Revision: https://phabricator.kde.org/D15265 --- plugins/scanfolder/scanthread.cpp | 5 +++++ plugins/scanfolder/scanthread.h | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/plugins/scanfolder/scanthread.cpp b/plugins/scanfolder/scanthread.cpp index 0e62768..d7b13e0 100644 --- a/plugins/scanfolder/scanthread.cpp +++ b/plugins/scanfolder/scanthread.cpp @@ -156,6 +156,11 @@ namespace kt void ScanThread::stop() { stop_requested = true; + + // XXX seems like deleting KDirWatch object(s) created in scan_folders + // in destructor of this QThread after it has been stopped + // causes memory corruption, so we delete them early + scan_folders.clear(); exit(); wait(); } diff --git a/plugins/scanfolder/scanthread.h b/plugins/scanfolder/scanthread.h index d13ca08..fb214a4 100644 --- a/plugins/scanfolder/scanthread.h +++ b/plugins/scanfolder/scanthread.h @@ -29,6 +29,7 @@ #include #include "scanfolder.h" +#include class QDir; @@ -88,8 +89,8 @@ namespace kt private: QMutex mutex; QStringList folders; - bool stop_requested; - bool recursive; + std::atomic stop_requested; + std::atomic recursive; bt::PtrMap scan_folders; }; -- cgit v0.11.2