<p>Pretty much the title I am trying to delete a few faces of a mesh using open mesh, like this:</p>
<pre><code>
    MyMesh mesh;

    char fname[1024];
    sprintf(fname, &quot;box_%i.obj&quot;, 0);
    if (!OpenMesh::IO::read_mesh(mesh, std::string(fname)))
    {
        std::cerr &lt;&lt; &quot;read error\n&quot;;
        exit(1);
    }

    MyMesh::FaceIter v_it, v_end(mesh.faces_end());
    uint count = 0;
    for (v_it=mesh.faces_begin(); v_it!=v_end; ++v_it)
    {
        mesh.delete_face(*v_it, true);
    }
</code></pre>
<p>This is segfaulting on the first call to delete_face.</p>
<p>However, writing this mesh (without trying to delete faces):</p>
<pre><code>    if (!OpenMesh::IO::write_mesh(mesh, std::string(&quot;open_mesh.obj&quot;)))
    {
        std::cerr &lt;&lt; &quot;write error\n&quot;;
        exit(1);
    }
</code></pre>
<p>Works perfectly fine and blender can open the obj. So the issue very much seems to be with how I am trying to delete the faces.</p>

CC BY-SA 4.0
2022-03-10T10:46:36.700Z
I don't think you can delete it like that, you're probably get a similar issue to the one you would have if you removed an element from a `std::list` via iterator in a for loop. Try to collect the iterators related in a `std::list` or `std::vector`, iterate through those for deletion and see what happens.
user8469759
CC BY-SA 4.0
2022-03-10T10:47:43.090Z
Or I guess you can also do a similar loop (pseudocode):
```while(mesh.numFaces > 0) {
      mesh.delete(mesh.faces_begin(),true));
   }
```
user8469759
<c++><computational-geometry><geometry><half-edge>
How to delete a face using open mesh?
Makogan
# How to delete a face using open mesh?

(Asked by: username_0 on 2022-03-06)

Pretty much the title I am trying to delete a few faces of a mesh using open mesh, like this:

```

    MyMesh mesh;

    char fname[1024];
    sprintf(fname, "box_%i.obj", 0);
    if (!OpenMesh::IO::read_mesh(mesh, std::string(fname)))
    {
        std::cerr << "read error\n";
        exit(1);
    }

    MyMesh::FaceIter v_it, v_end(mesh.faces_end());
    uint count = 0;
    for (v_it=mesh.faces_begin(); v_it!=v_end; ++v_it)
    {
        mesh.delete_face(*v_it, true);
    }

```

This is segfaulting on the first call to delete\_face.

However, writing this mesh (without trying to delete faces):

```
    if (!OpenMesh::IO::write_mesh(mesh, std::string("open_mesh.obj")))
    {
        std::cerr << "write error\n";
        exit(1);
    }

```

Works perfectly fine and blender can open the obj. So the issue very much seems to be with how I am trying to delete the faces.

**Tags:** c++, computational-geometry, geometry, half-edge

> **Comments:**
> **username_1**: I don't think you can delete it like that, you're probably get a similar issue to the one you would have if you removed an element from a `std::list` via iterator in a for loop. Try to collect the iterators related in a `std::list` or `std::vector`, iterate through those for deletion and see what happens.  
> **username_1**: Or I guess you can also do a similar loop (pseudocode): ```while(mesh.numFaces > 0) {       mesh.delete(mesh.faces_begin(),true));    } ```  

---

## 0 answers

