commit 9a9288d67dac1cdbd76372b1c0e0b7a25708cc09
Author: Guillaume Munch <[email protected]>
Date: Sat Dec 31 15:16:15 2016 +0100
Fix memory leak with WordLists
https://www.mail-archive.com/[email protected]/msg198230.html
---
src/LyX.cpp | 1 -
src/WordList.cpp | 30 ++++++++----------------------
src/WordList.h | 2 --
3 files changed, 8 insertions(+), 25 deletions(-)
diff --git a/src/LyX.cpp b/src/LyX.cpp
index 047e265..b10b60b 100644
--- a/src/LyX.cpp
+++ b/src/LyX.cpp
@@ -234,7 +234,6 @@ LyX::~LyX()
{
delete pimpl_;
singleton_ = 0;
- WordList::cleanupWordLists();
}
diff --git a/src/WordList.cpp b/src/WordList.cpp
index 8180985..9bc52ac 100644
--- a/src/WordList.cpp
+++ b/src/WordList.cpp
@@ -16,6 +16,7 @@
#include "support/debug.h"
#include "support/docstring.h"
#include "support/lassert.h"
+#include "support/unique_ptr.h"
#include "support/weighted_btree.h"
#include <QThreadStorage>
@@ -27,7 +28,7 @@ using namespace std;
namespace lyx {
///
-typedef map<string, WordList *> GlobalWordList;
+typedef map<string, unique_ptr<WordList>> GlobalWordList;
// Each thread uses its own word list, but only the one of the GUI thread is
// used to do real work. The others are only neded to prevent simultanous
// write access e.g. from a cloned buffer and a true document buffer.
@@ -38,27 +39,12 @@ WordList & theWordList(string const & lang)
{
if (!theGlobalWordList.hasLocalData())
theGlobalWordList.setLocalData(new GlobalWordList);
- GlobalWordList * globalWordList = theGlobalWordList.localData();
- GlobalWordList::iterator it = globalWordList->find(lang);
- if (it != globalWordList->end())
+ GlobalWordList & globalWordList = *theGlobalWordList.localData();
+ GlobalWordList::iterator it = globalWordList.find(lang);
+ if (it != globalWordList.end())
return *it->second;
- else {
- WordList * wl = new WordList;
- (*globalWordList)[lang] = wl;
- return *wl;
- }
-}
-
-
-void WordList::cleanupWordLists()
-{
- if (!theGlobalWordList.hasLocalData())
- return;
- GlobalWordList * globalWordList = theGlobalWordList.localData();
- GlobalWordList::const_iterator it = globalWordList->begin();
- for (; it != globalWordList->end(); ++it)
- delete it->second;
- globalWordList->clear();
+ else
+ return *(globalWordList[lang] = make_unique<WordList>());
}
@@ -73,7 +59,7 @@ struct WordList::Impl {
};
-WordList::WordList() : d(new Impl)
+WordList::WordList() : d(make_unique<Impl>())
{
d->c_ = 0;
diff --git a/src/WordList.h b/src/WordList.h
index 0c5c0a8..2afd6cb 100644
--- a/src/WordList.h
+++ b/src/WordList.h
@@ -30,8 +30,6 @@ public:
void insert(docstring const & w);
///
void remove(docstring const & w);
- ///
- static void cleanupWordLists();
private:
struct Impl;