[gnote] Fix memory leak in TrieTree



commit f47eac8deb464d83fe6a5002a8d2be87d7d9370d
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Thu Sep 8 15:58:42 2016 +0300

    Fix memory leak in TrieTree
    
    Circular references. Instead use simple pointers and keep all states in
    a list. Whenever any changes occurr, trie is recreated and will delete
    all states in it's destructor.
    
    Fixes Bug 770056.

 src/trie.hpp |   16 +++++++++++++---
 1 files changed, 13 insertions(+), 3 deletions(-)
---
diff --git a/src/trie.hpp b/src/trie.hpp
index 14c4477..b7e079e 100644
--- a/src/trie.hpp
+++ b/src/trie.hpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2013-2014 Aurimas Cernius
+ * Copyright (C) 2013-2014,2016 Aurimas Cernius
  * Copyright (C) 2011 Debarshi Ray
  * Copyright (C) 2009 Hubert Figuiere
  *
@@ -39,7 +39,7 @@ class TrieTree
 private:
 
   class TrieState;
-  typedef shared_ptr<TrieState> TrieStatePtr;
+  typedef TrieState* TrieStatePtr;
   typedef std::list<TrieStatePtr> TrieStateList;
   typedef std::queue<TrieStatePtr> TrieStateQueue;
 
@@ -112,6 +112,7 @@ private:
     bool m_payload_present;
   };
 
+  std::vector<TrieState*> m_states;
   const bool m_case_sensitive;
   const TrieStatePtr m_root;
   size_t m_max_length;
@@ -123,6 +124,14 @@ public:
     , m_root(new TrieState('\0', -1, TrieStatePtr()))
     , m_max_length(0)
   {
+    m_states.push_back(m_root);
+  }
+
+  ~TrieTree()
+  {
+    for(auto state : m_states) {
+      delete state;
+    }
   }
 
   void add_keyword(const Glib::ustring & keyword, const value_t & pattern_id)
@@ -136,7 +145,8 @@ public:
 
       TrieStatePtr target_state = find_state_transition(current_state, c);
       if (0 == target_state) {
-        target_state = TrieStatePtr(new TrieState(c, i, m_root));
+        target_state = new TrieState(c, i, m_root);
+        m_states.push_back(target_state);
         current_state->transitions().push_front(target_state);
       }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]