Browse Source

Fixes a race condition in the dual contouring code. (#2045)

The previous version of the code would call new_vertex() outside of the mutex, which would trigger a call to resize some std::vector<>s.
This addresses the issue by moving the vertex such that the initialization of ev is contained within the mutex.
ubc-nvining 3 years ago
parent
commit
3370a3e9ca
1 changed files with 11 additions and 7 deletions
  1. 11 7
      include/igl/dual_contouring.cpp

+ 11 - 7
include/igl/dual_contouring.cpp

@@ -214,14 +214,18 @@ namespace igl
           t = (isovalue - f0)/delta;
           p = e0+t*(e1-e0);
         }
-        // insert vertex at this point to triangulate quad face
-        const typename decltype(V)::Index ev = triangles ? new_vertex() : -1;
-        if(triangles)
+        typename decltype(V)::Index ev;
+
         {
-          const std::lock_guard<std::mutex> lock(Vmut);
-          vV[ev] = p;
-          vcount[ev] = 1;
-          vI[ev] = Eigen::RowVector3i(-1,-1,-1);
+            const std::lock_guard<std::mutex> lock(Vmut);
+            // insert vertex at this point to triangulate quad face
+            ev = triangles ? new_vertex() : -1;
+            if (triangles)
+            {
+                vV[ev] = p;
+                vcount[ev] = 1;
+                vI[ev] = Eigen::RowVector3i(-1, -1, -1);
+            }
         }
         // edge normal from function handle (could use grid finite
         // differences/interpolation gradients)