From 7657d38ec149028dd3ffcd2b92e08c4ffcb7abdb Mon Sep 17 00:00:00 2001
From: Jonathan Lambrechts <jonathan.lambrechts@uclouvain.be>
Date: Tue, 20 Apr 2010 22:19:20 +0000
Subject: [PATCH] dg : remove major memory leaks

---
 Common/LuaBindings.h | 25 +++++++++++++++++++++----
 Solver/function.cpp  |  6 ++++--
 2 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/Common/LuaBindings.h b/Common/LuaBindings.h
index f8a55bb9df..5d9aae5e01 100644
--- a/Common/LuaBindings.h
+++ b/Common/LuaBindings.h
@@ -893,6 +893,17 @@ class constructorBindingT<tObj, t0, t1, t2, t3, void> : public luaMethodBinding
   }
 };
 
+template <class t>
+class destructorBindingT : public luaMethodBinding {
+ public:
+  int call(lua_State *L)
+  {
+    t *o = luaStack<t*>::get(L,1);
+    delete o;
+    return 0;
+  }
+};
+
 class classBinding {
   std::string _className;
   binding *_b;
@@ -997,10 +1008,7 @@ class classBinding {
   inline const std::string getDescription() const { return _description; }
   inline classBinding *getParent() const { return _parent; }
   std::map<std::string, luaMethodBinding *> methods;
-  template <typename cb>
-  methodBinding *addMethod(std::string n, cb f)
-  {
-    luaMethodBinding *mb = new methodBindingT<cb>(n, f);
+  void addMethodLua (std::string n, luaMethodBinding *mb) {
     methods[n] = mb;
     lua_State *L = _b->L;
     lua_getglobal(L, _className.c_str());
@@ -1013,6 +1021,12 @@ class classBinding {
     lua_pushcclosure(L, callMethod, 1);
     lua_setfield(L,methods, n.c_str()); //className.name = callMethod(mb)
     lua_pop(L, 1);
+  }
+  template <typename cb>
+  methodBinding *addMethod(std::string n, cb f)
+  {
+    luaMethodBinding *mb = new methodBindingT<cb>(n, f);
+    addMethodLua(n,mb);
     return mb; 
   }
   template <typename tObj, typename t0, typename t1, typename t2, typename t3, 
@@ -1073,6 +1087,9 @@ classBinding *binding::addClass(std::string name)
 {
   className<t>::set(name);
   classBinding *cb = new classBinding(this, name);
+  luaMethodBinding *d = new destructorBindingT<t>();
+  d->setDescription("destructor");
+  cb->addMethodLua("delete", d);
   classes[name] = cb;
   return cb;
 }
diff --git a/Solver/function.cpp b/Solver/function.cpp
index 2104c5bdde..2418fe4dc7 100644
--- a/Solver/function.cpp
+++ b/Solver/function.cpp
@@ -51,8 +51,7 @@ dataCacheDouble::dataCacheDouble(dataCacheMap *m, function *f):
   m->addDataCacheDouble(this, f->isInvalitedOnElement());
   _function = f;
   for(int i=0; i<f->_childrenCache.size(); i++) {
-    dataCacheMap *m2 = m->newChild();
-    m->addSecondaryCache(m2);
+    m->addSecondaryCache(m->newChild());
   }
   _substitutions.resize(f->_substitutedFunctions.size());
   for(int i=0; i<f->_substitutedFunctions.size(); i++) {
@@ -522,5 +521,8 @@ functionReplaceCache::functionReplaceCache(dataCacheMap *m, functionReplace *rep
   }
 }
 functionReplaceCache::~functionReplaceCache() {
+  for (int i = 0; i< map->_secondaryCaches.size(); i++) {
+    delete map->_secondaryCaches[i];
+  }
   delete map;
 }
-- 
GitLab