diff --git a/Common/onelab.h b/Common/onelab.h
index 8a9dc5cfbed349b99dd0203905b1cda99c8f0206..1b9c24a57dd58ba305cf2690593085f41e00c6c9 100644
--- a/Common/onelab.h
+++ b/Common/onelab.h
@@ -979,13 +979,17 @@ namespace onelab{
                                    std::set<T*, parameterLessThan> &ps)
     {
       if(name.empty() && client.size()){
+        std::vector<T*> toDelete;
         for(typename std::set<T*, parameterLessThan>::iterator it = ps.begin();
-            it != ps.end(); it++){
+            it != ps.end(); ){
           T *p = *it;
           if(p->hasClient(client)){
-            ps.erase(it);
+            ps.erase(it++); // to avoid invalid iterator
             delete p;
           }
+          else{
+            it++;
+          }
         }
       }
       else{
diff --git a/Geo/Geo.cpp b/Geo/Geo.cpp
index 75be98c0d200c8e1447d1006d01974576cb3c319..e8e35786c301ef6d31b882a701a01be352a83977 100644
--- a/Geo/Geo.cpp
+++ b/Geo/Geo.cpp
@@ -2534,7 +2534,8 @@ static void ReplaceDuplicatePoints(std::map<int, int> * v_report = 0)
         std::map<int, int>::iterator m_it = v_report->find(v->Num);
         if(m_it != v_report->end()){
 	  Vertex **v_rep = (Vertex **)Tree_PQuery(allNonDuplicatedPoints, &v);
-	  m_it->second = (*v_rep)->Num;
+          if(v_rep)
+            m_it->second = (*v_rep)->Num;
         }
       }
     }
@@ -2711,12 +2712,14 @@ static void ReplaceDuplicateCurves(std::map<int, int> * c_report = 0)
 	  std::map<int, int>::iterator m_it = c_report->find(c->Num);
 	  if(m_it != c_report->end()){
 	    Curve **c_rep = (Curve **)Tree_PQuery(allNonDuplicatedCurves, &c);
-	    m_it->second = (*c_rep)->Num;
+            if(c_rep)
+              m_it->second = (*c_rep)->Num;
 	  }
 	  m_it = c_report->find(c2->Num);
 	  if(m_it != c_report->end()){
 	    Curve **c_rep_neg = (Curve **)Tree_PQuery(allNonDuplicatedCurves, &c2);
-	    m_it->second = (*c_rep_neg)->Num;
+            if(c_rep_neg)
+              m_it->second = (*c_rep_neg)->Num;
 	  }
 	}
       }
@@ -3005,7 +3008,8 @@ static void ReplaceDuplicateSurfaces(std::map<int, int> *s_report = 0)
 	  std::map<int, int>::iterator m_it = (*s_report).find(s->Num);
 	  if(m_it != s_report->end()){
 	    Surface **s_rep = (Surface **)Tree_PQuery(allNonDuplicatedSurfaces, &s);
-	    m_it->second = (*s_rep)->Num;
+            if(s_rep)
+              m_it->second = (*s_rep)->Num;
 	  }
 	}
       }
diff --git a/Geo/gmshEdge.cpp b/Geo/gmshEdge.cpp
index 088178764531ff971005f22cfe768f37a2f6397c..40aadcadc2ebb2121cdf8eb0f17b8c9b403d8eee 100644
--- a/Geo/gmshEdge.cpp
+++ b/Geo/gmshEdge.cpp
@@ -407,7 +407,7 @@ void gmshEdge::writeGEO(FILE *fp)
       fprintf(fp, ", %d", v->Num);
     else
       fprintf(fp, "{%d", v->Num);
-    if(i % 6 == 7)
+    if(i % 8 == 7)
       fprintf(fp, "\n");
   }
   fprintf(fp, "};\n");
diff --git a/Post/shapeFunctions.h b/Post/shapeFunctions.h
index 6a2a94aeac632058581089be89ea970e4c234e15..d17286c8cde00fe450afefc0bf3365a4a0362bcc 100644
--- a/Post/shapeFunctions.h
+++ b/Post/shapeFunctions.h
@@ -912,7 +912,7 @@ public:
   }
   void getGradShapeFunction(int num, double u, double v, double w, double s[3])
   {
-    if(w == 1. && num != 4) {
+    if(w == 1.) {
       switch(num) {
       case 0 : s[0] = -0.25 ;
                s[1] = -0.25 ;