on xenial help sign in
use QPointer to track singleton destruction
9f4cd46Nick Dedekind
plugins/Unity/Indicators/unitymenumodelcache.cpp+3−3
@@ -22,14 +22,14 @@
22
22
23#include <QQmlEngine>
23#include <QQmlEngine>
24
24
25−UnityMenuModelCache* UnityMenuModelCache::theCache = nullptr;
25+QPointer<UnityMenuModelCache> UnityMenuModelCache::theCache = nullptr;
26
26
27UnityMenuModelCache* UnityMenuModelCache::singleton()
27UnityMenuModelCache* UnityMenuModelCache::singleton()
28{
28{
29− if (!theCache) {
29+ if (theCache.isNull()) {
30 theCache = new UnityMenuModelCache();
30 theCache = new UnityMenuModelCache();
31 }
31 }
32− return theCache;
32+ return theCache.data();
33}
33}
34
34
35UnityMenuModelCache::UnityMenuModelCache(QObject* parent)
35UnityMenuModelCache::UnityMenuModelCache(QObject* parent)
plugins/Unity/Indicators/unitymenumodelcache.h+2−1
@@ -24,6 +24,7 @@
24
24
25#include <QObject>
25#include <QObject>
26#include <QHash>
26#include <QHash>
27+#include <QPointer>
27#include <QWeakPointer>
28#include <QWeakPointer>
28
29
29class UnityMenuModel;
30class UnityMenuModel;
@@ -45,7 +46,7 @@ public:
45
46
46protected:
47protected:
47 QHash<QByteArray, QWeakPointer<UnityMenuModel>> m_registry;
48 QHash<QByteArray, QWeakPointer<UnityMenuModel>> m_registry;
48− static UnityMenuModelCache* theCache;
49+ static QPointer<UnityMenuModelCache> theCache;
49};
50};
50
51
51#endif // UNITYMENUMODELCACHE_H
52#endif // UNITYMENUMODELCACHE_H
tests/mocks/Unity/Indicators/fakeunitymenumodelcache.cpp+3−3
@@ -17,14 +17,14 @@
17#include "fakeunitymenumodelcache.h"
17#include "fakeunitymenumodelcache.h"
18#include <unitymenumodel.h>
18#include <unitymenumodel.h>
19
19
20−FakeUnityMenuModelCache* FakeUnityMenuModelCache::theFakeCache = nullptr;
20+QPointer<FakeUnityMenuModelCache> FakeUnityMenuModelCache::theFakeCache = nullptr;
21
21
22FakeUnityMenuModelCache* FakeUnityMenuModelCache::singleton()
22FakeUnityMenuModelCache* FakeUnityMenuModelCache::singleton()
23{
23{
24− if (!theFakeCache) {
24+ if (theFakeCache.isNull()) {
25 theFakeCache = new FakeUnityMenuModelCache();
25 theFakeCache = new FakeUnityMenuModelCache();
26 }
26 }
27− return theFakeCache;
27+ return theFakeCache.data();
28}
28}
29
29
30FakeUnityMenuModelCache::FakeUnityMenuModelCache(QObject* parent)
30FakeUnityMenuModelCache::FakeUnityMenuModelCache(QObject* parent)
tests/mocks/Unity/Indicators/fakeunitymenumodelcache.h+1−2
@@ -19,7 +19,6 @@
19
19
20#include "unitymenumodelcache.h"
20#include "unitymenumodelcache.h"
21
21
22−#include <QHash>
23#include <QVariantMap>
22#include <QVariantMap>
24
23
25class FakeUnityMenuModelCache : public UnityMenuModelCache
24class FakeUnityMenuModelCache : public UnityMenuModelCache
@@ -43,7 +42,7 @@ public:
43 const QVariant& data = QVariant());
42 const QVariant& data = QVariant());
44
43
45private:
44private:
46− static FakeUnityMenuModelCache* theFakeCache;
45+ static QPointer<FakeUnityMenuModelCache> theFakeCache;
47 QHash<QByteArray, QSharedPointer<UnityMenuModel>> m_models;
46 QHash<QByteArray, QSharedPointer<UnityMenuModel>> m_models;
48};
47};
49
48
tests/plugins/Unity/Indicators/sharedunitymenumodeltest.cpp+10
@@ -86,6 +86,16 @@ private Q_SLOTS:
86 QCOMPARE(model2->model()->menuObjectPath(), QByteArray("/com/canonical/LP1328646"));
86 QCOMPARE(model2->model()->menuObjectPath(), QByteArray("/com/canonical/LP1328646"));
87 }
87 }
88
88
89+ // Tests that the cache is recreated if deleted.
90+ void testDeletedCache()
91+ {
92+ QSharedPointer<SharedUnityMenuModel> model1(createFullModel("test1"));
93+
94+ QCOMPARE(UnityMenuModelCache::singleton()->contains("/com/canonical/test1"), true);
95+ delete UnityMenuModelCache::singleton();
96+ QCOMPARE(UnityMenuModelCache::singleton()->contains("/com/canonical/test1"), false);
97+ }
98+
89};
99};
90
100
91QTEST_GUILESS_MAIN(SharedUnityMenuModelTest)
101QTEST_GUILESS_MAIN(SharedUnityMenuModelTest)