# Python Seg Fault

**URL:** https://discourse.vtk.org/t/python-seg-fault/3353
**Category:** Development
**Created:** [May 20, 2020, 4:46pm UTC](https://discourse.vtk.org/t/python-seg-fault/3353 "2020-05-20T16:46:31Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![gatti](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/g/eb9ed0/32.png) [@gatti](https://discourse.vtk.org/u/gatti)
#### Post date: [May 20, 2020, 4:46pm UTC](https://discourse.vtk.org/t/python-seg-fault/3353/1 "2020-05-20T16:46:31Z")

</div>

Hi,

I’m messaging because I believe I came across a bug. I’ve found a hacky fix for it, but I don’t think my fix is the correct long-term solution. Im using vtk==8.2.0 installed from Conda.

If you `filter.GetOutput()` from some filter and then try to delete a cell `polydata.DeleteCell(idx)` of the outputted polydata you get a seg fault. However, if you simply interact with the cells in some (any?) way and then perform the delete there is no error (e.g., by doing `polydata.GetCell(idx)` and then do `polydata.DeleteCell(idx)`; note that I don’t think idx has to be the same between these, and `.GetCell()` only needs to be done once and then you can iterate over the entire mesh and delete whatever cells you want.

Basic/reproducible example code:  
ERROR:

```auto
import vtk
sphereSource = vtk.vtkSphereSource()
sphereSource.SetCenter(0.0, 0.0, 0.0)
sphereSource.SetRadius(5.0)
sphereSource.SetPhiResolution(100)
sphereSource.SetThetaResolution(100)
sphereSource.Update()

sphere = vtk.vtkPolyData()
sphere.DeepCopy(sphereSource.GetOutput())

sphere.DeleteCell(1)

```

RESOLUTION:

```auto
import vtk
sphereSource = vtk.vtkSphereSource()
sphereSource.SetCenter(0.0, 0.0, 0.0)
sphereSource.SetRadius(5.0)
sphereSource.SetPhiResolution(100)
sphereSource.SetThetaResolution(100)
sphereSource.Update()

sphere = vtk.vtkPolyData()
sphere.DeepCopy(sphereSource.GetOutput())

sphere.GetCell(1)
sphere.DeleteCell(1)

```

Note that it doesn’t matter if I `DeepCopy` the output of the filter or not.

Clearly, the fix is extremely simple and I can easily include this in any of my code. However, without knowing this is the problem it could be a pain to sort out.

Let me know if any additional information is of use.

Thanks,

Anthony.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [May 20, 2020, 4:59pm UTC](https://discourse.vtk.org/t/python-seg-fault/3353/2 "2020-05-20T16:59:42Z")

</div>

Certain methods require that `BuildLinks()` is called beforehand. See the [vtkPolyData](https://vtk.org/doc/nightly/html/classvtkPolyData.html) docs for more info.

```auto
sphere.EditableOn()
sphere.BuildLinks()
sphere.DeleteCell(1)

```

Edit: The polydata should be marked Editable, too, so I’ve added that.

---

<div class="post-metadata">

### Author: ![gatti](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/g/eb9ed0/32.png) [@gatti](https://discourse.vtk.org/u/gatti)
#### Post date: [May 20, 2020, 5:09pm UTC](https://discourse.vtk.org/t/python-seg-fault/3353/3 "2020-05-20T17:09:16Z")

</div>

Thanks, David.

Appreciate the prompt reply. Im assuming that by calling `.GetCell()` that `.BuildLinks()` is called as a part of it - hence why my hack works.

I was going to ask something about having `.BuildLinks()` run by default with `.GetOutput()`, but from reading through the docs more it seems like some functions require it isn’t called yet.

I guess something you just need to learn once along the path to know to look for it as something that is needed to be done.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [May 20, 2020, 5:32pm UTC](https://discourse.vtk.org/t/python-seg-fault/3353/4 "2020-05-20T17:32:48Z")

</div>

BuildLinks() isn’t called automatically because it is expensive and is only required in certain situations.
