on xenial help sign in
Added cachedunitymenumodel reference counting
commit54f4d73b51437ef4f167942347304e31ff95e50d authorNick Dedekind <nicholas.dedekind@gmail.com> dateThu 29 Aug 2013, 15:06 (13 years ago) parentdb61a4c
3 files changed+37−1
plugins/Unity/Indicators/cachedunitymenumodel.cpp+11−1 plugins/Unity/Indicators/unitymenumodelcache.cpp+22 plugins/Unity/Indicators/unitymenumodelcache.h+4
plugins/Unity/Indicators/cachedunitymenumodel.cpp+11−1
@@ -31,6 +31,11 @@ CachedUnityMenuModel::CachedUnityMenuModel(QObject* parent)
3131
3232CachedUnityMenuModel::~CachedUnityMenuModel()
3333{
34+ // deref old model.
35+ if (m_model) {
36+ UnityMenuModelCache::cache()->deref(m_menuObjectPath);
37+ m_model = NULL;
38+ }
3439}
3540
3641UnityMenuModel* CachedUnityMenuModel::model() const
@@ -80,7 +85,11 @@ QString CachedUnityMenuModel::menuObjectPath() const
8085void CachedUnityMenuModel::setMenuObjectPath(const QString &path)
8186{
8287 if (m_menuObjectPath != path) {
83− m_model = NULL;
88+ // deref old model.
89+ if (m_model) {
90+ UnityMenuModelCache::cache()->deref(m_menuObjectPath);
91+ m_model = NULL;
92+ }
8493
8594 m_menuObjectPath = path;
8695 Q_EMIT menuObjectPathChanged(m_menuObjectPath);
@@ -96,6 +105,7 @@ void CachedUnityMenuModel::setMenuObjectPath(const QString &path)
96105
97106 UnityMenuModelCache::cache()->registerModel(m_menuObjectPath, m_model);
98107 }
108+ UnityMenuModelCache::cache()->ref(m_menuObjectPath);
99109 }
100110
101111 Q_EMIT modelChanged(m_model);
plugins/Unity/Indicators/unitymenumodelcache.cpp+22
@@ -46,3 +46,25 @@ UnityMenuModelCache* UnityMenuModelCache::cache()
4646 static UnityMenuModelCache* model = new UnityMenuModelCache;
4747 return model;
4848}
49+
50+void UnityMenuModelCache::ref(const QString& path)
51+{
52+ if (!m_refs.contains(path)) {
53+ m_refs[path] = 1;
54+ } else {
55+ m_refs[path]++;
56+ }
57+}
58+
59+bool UnityMenuModelCache::deref(const QString& path)
60+{
61+ if (m_refs.contains(path)) {
62+ if (--m_refs[path] <= 0) {
63+ delete m_menuModels.value(path, NULL);
64+ m_menuModels.remove(path);
65+ m_refs.remove(path);
66+ return false;
67+ }
68+ }
69+ return true;
70+}
plugins/Unity/Indicators/unitymenumodelcache.h+4
@@ -37,10 +37,14 @@ public:
3737 Q_INVOKABLE UnityMenuModel* model(const QString& path) const;
3838 Q_INVOKABLE void registerModel(const QString& path, UnityMenuModel* menuModel);
3939
40+ void ref(const QString& path);
41+ bool deref(const QString& path);
42+
4043 static UnityMenuModelCache* cache();
4144
4245private:
4346 QHash<QString, UnityMenuModel*> m_menuModels;
47+ QHash<QString, int> m_refs;
4448};
4549
4650#endif // UNITYMENUMODELCACHE_H