summaryrefslogtreecommitdiff
path: root/kde-frameworks/knewstuff/files/knewstuff-5.77.0-add-dptr-to-cache.patch
blob: f6547fc6e5ffb6dfabdf350a07e945dc61989f14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
From 243ea6155b28457c8b1441fee8ab1037828d21ba Mon Sep 17 00:00:00 2001
From: Dan Leinir Turthra Jensen <admin@leinir.dk>
Date: Mon, 14 Dec 2020 21:11:51 +0000
Subject: [PATCH] Add a dptr to Cache, and move the throttle timer there to fix
 crash

Previously, the throttle timer was a raw static, but it was also a parented qobject, which means that when the cache was deleted, so was the timer, but the variable was not reset. Consequently, things would crash left and right later on. So, to alleviate this, and hopefully avoid future issues, introduce a dptr, stick the timer there, and move the logic to that private class as well.

BUG:429442

FIXED-IN:5.78
---
 src/core/cache.cpp | 41 ++++++++++++++++++++++++++++++-----------
 src/core/cache.h   |  7 +++++--
 2 files changed, 35 insertions(+), 13 deletions(-)

diff --git a/src/core/cache.cpp b/src/core/cache.cpp
index 0395045c..ace7be4e 100644
--- a/src/core/cache.cpp
+++ b/src/core/cache.cpp
@@ -11,17 +11,42 @@
 #include <QDir>
 #include <QFileInfo>
 #include <QFileSystemWatcher>
+#include <QPointer>
 #include <QTimer>
 #include <QXmlStreamReader>
 #include <qstandardpaths.h>
 #include <knewstuffcore_debug.h>
 
+class KNSCore::CachePrivate {
+public:
+    CachePrivate(Cache* qq)
+        : q(qq)
+    {}
+    ~CachePrivate() {}
+
+    Cache* q;
+    QHash<QString, EntryInternal::List> requestCache;
+
+    QPointer<QTimer> throttleTimer;
+    void throttleWrite() {
+        if (!throttleTimer) {
+            throttleTimer = new QTimer(q);
+            QObject::connect(throttleTimer, &QTimer::timeout, q, [this](){ q->writeRegistry(); });
+            throttleTimer->setSingleShot(true);
+            throttleTimer->setInterval(1000);
+        }
+        throttleTimer->start();
+    }
+};
+
 using namespace KNSCore;
 
 typedef QHash<QString, QWeakPointer<Cache> > CacheHash;
 Q_GLOBAL_STATIC(CacheHash, s_caches)
 
-Cache::Cache(const QString &appName): QObject(nullptr)
+Cache::Cache(const QString &appName)
+    : QObject(nullptr)
+    , d(new CachePrivate(this))
 {
     m_kns2ComponentName = appName;
 
@@ -280,36 +305,30 @@ void Cache::registerChangedEntry(const KNSCore::EntryInternal &entry)
     if (entry.status() == KNS3::Entry::Updating || entry.status() == KNS3::Entry::Installing) {
         return;
     }
-    static QTimer* writeThrottle{nullptr};
-    if (!writeThrottle) {
-        writeThrottle = new QTimer(this);
-        connect(writeThrottle, &QTimer::timeout, this, [this](){ writeRegistry(); });
-        writeThrottle->setInterval(1000);
-    }
     if (!property("reloadingRegistry").toBool()) {
         setProperty("dirty", true);
         cache.remove(entry); // If value already exists in the set, the set is left unchanged
         cache.insert(entry);
-        writeThrottle->start();
+        d->throttleWrite();
     }
 }
 
 void Cache::insertRequest(const KNSCore::Provider::SearchRequest &request, const KNSCore::EntryInternal::List &entries)
 {
     // append new entries
-    auto &cacheList = requestCache[request.hashForRequest()];
+    auto &cacheList = d->requestCache[request.hashForRequest()];
     for (const auto &entry : entries) {
         if (!cacheList.contains(entry)) {
             cacheList.append(entry);
         }
     }
-    qCDebug(KNEWSTUFFCORE) << request.hashForRequest() << " add: " << entries.size() << " keys: " << requestCache.keys();
+    qCDebug(KNEWSTUFFCORE) << request.hashForRequest() << " add: " << entries.size() << " keys: " << d->requestCache.keys();
 }
 
 EntryInternal::List Cache::requestFromCache(const KNSCore::Provider::SearchRequest &request)
 {
     qCDebug(KNEWSTUFFCORE) << request.hashForRequest();
-    return requestCache.value(request.hashForRequest());
+    return d->requestCache.value(request.hashForRequest());
 }
 
 void KNSCore::Cache::removeDeletedEntries()
diff --git a/src/core/cache.h b/src/core/cache.h
index 06e95ab4..73ea7c61 100644
--- a/src/core/cache.h
+++ b/src/core/cache.h
@@ -16,9 +16,11 @@
 
 #include "knewstuffcore_export.h"
 
+#include <memory.h>
+
 namespace KNSCore
 {
-
+class CachePrivate;
 class KNEWSTUFFCORE_EXPORT Cache : public QObject
 {
     Q_OBJECT
@@ -99,7 +101,8 @@ private:
     QString m_kns2ComponentName;
 
     QSet<EntryInternal> cache;
-    QHash<QString, EntryInternal::List> requestCache;
+
+    std::unique_ptr<CachePrivate> d;
 };
 
 }
-- 
GitLab