From 11c4d4821ac00bfe83483ea67eb4d11798749553 Mon Sep 17 00:00:00 2001
From: Michel Rasquin <michel.rasquin@cenaero.be>
Date: Wed, 20 Jan 2016 16:52:40 +0000
Subject: [PATCH] Fix for clean memory deallocation

---
 Common/Gmsh.cpp    | 11 ++++++++++-
 Post/PViewData.cpp | 11 +++++++++++
 Post/PViewData.h   |  1 +
 3 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/Common/Gmsh.cpp b/Common/Gmsh.cpp
index c4a1449a92..c4c7324780 100644
--- a/Common/Gmsh.cpp
+++ b/Common/Gmsh.cpp
@@ -23,6 +23,7 @@
 #include "robustPredicates.h"
 #if defined(HAVE_POST)
 #include "PView.h"
+#include "PViewData.h"
 #endif
 
 #if defined(HAVE_ONELAB) && !defined(HAVE_ONELAB2)
@@ -230,10 +231,18 @@ int GmshWriteFile(const std::string &fileName)
 int GmshFinalize()
 {
 #if defined(HAVE_POST)
+  // Delete all PViewData stored in static PView list
+  for(unsigned int i = 0; i < PView::list.size(); i++) {
+    delete PView::list[i];
+  }
   PView::list.clear();
+  // Delete static interpolationSchemes
+  PViewData::removeAllInterpolationSchemes();  
 #endif
+  // Delete all Gmodels
+  for(unsigned int i = 0; i < GModel::list.size(); i++)
+    delete GModel::list[i];
   GModel::list.clear();
-  CTX::instance()->files.clear();
   return 1;
 }
 
diff --git a/Post/PViewData.cpp b/Post/PViewData.cpp
index 903f860471..f08cdf0032 100644
--- a/Post/PViewData.cpp
+++ b/Post/PViewData.cpp
@@ -196,6 +196,17 @@ void PViewData::removeInterpolationScheme(const std::string &name)
   }
 }
 
+void PViewData::removeAllInterpolationSchemes()
+{
+  std::map<std::string, interpolationMatrices>::iterator it = _interpolationSchemes.begin();
+  for(;it!=_interpolationSchemes.end();it++)
+    for(interpolationMatrices::iterator it2 = it->second.begin();
+        it2 != it->second.end(); it2++)
+        for(unsigned int i = 0; i < it2->second.size(); i++)
+          delete it2->second[i];
+  _interpolationSchemes.clear();
+}
+
 void PViewData::addMatrixToInterpolationScheme(const std::string &name, int type,
                                                fullMatrix<double> &mat)
 {
diff --git a/Post/PViewData.h b/Post/PViewData.h
index 9a90bcd74f..085958010d 100644
--- a/Post/PViewData.h
+++ b/Post/PViewData.h
@@ -228,6 +228,7 @@ class PViewData {
 
   // access to global interpolation schemes
   static void removeInterpolationScheme(const std::string &name);
+  static void removeAllInterpolationSchemes();
   static void addMatrixToInterpolationScheme(const std::string &name, int type,
                                              fullMatrix<double> &mat);
   static int getSizeInterpolationScheme() {return _interpolationSchemes.size();}
-- 
GitLab