# Seg faults on Render() call

**URL:** https://discourse.vtk.org/t/seg-faults-on-render-call/9803
**Category:** Development
**Created:** [November 11, 2022, 7:15pm UTC](https://discourse.vtk.org/t/seg-faults-on-render-call/9803 "2022-11-11T19:15:30Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![marcomusy](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marcomusy/32/95_2.png) [@marcomusy](https://discourse.vtk.org/u/marcomusy)
#### Post date: [November 11, 2022, 7:15pm UTC](https://discourse.vtk.org/t/seg-faults-on-render-call/9803/1 "2022-11-11T19:15:30Z")

</div>

I’m getting seg fault in my Ubuntu system:

```bash
vtk version : 9.2.2
python version : 3.9.13 (main, Aug 25 2022, 23:26:10) [GCC 11.2.0]
python interpreter: /home/musy/soft/anaconda3/bin/python
system : Linux 5.4.0-131-generic posix x86_64

```

with

```python
import vtk
import time

cone = vtk.vtkConeSource()

mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(cone.GetOutputPort())

actor = vtk.vtkActor()
actor.SetMapper(mapper)

ren = vtk.vtkRenderer()
ren.SetBackground(0.1, 0.2, 0.4)
ren.AddActor(actor)

renwin = vtk.vtkRenderWindow()
renwin.SetSize(500, 500)
renwin.AddRenderer(ren)

iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renwin)

# renwin.Render()
# iren.Initialize()
iren.Start()

print("sleep")
time.sleep(1)
print("slept")

iren.Render() # Segmentation fault (core dumped)
iren.Start() # is always ignored

```

VTK 9.0.3 the same works fine as expected.

---

<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: [November 12, 2022, 2:52pm UTC](https://discourse.vtk.org/t/seg-faults-on-render-call/9803/2 "2022-11-12T14:52:03Z")

</div>

I can also confirm that this regression did not exist in VTK 9.1.0, but it does exist in the VTK master branch.

Here’s a partial stack trace from where the vtkXOpenGLRenderWindow is attempting to create a new X Window to replace the one that had previously been destroyed:

```nohighlight
10 vtk9::vtkXOpenGLRenderWindow::CreateAWindow() + 220
9 vtk9::vtkXOpenGLRenderWindow::GetDesiredVisualInfo() + 99
8 lib/libvtkRenderingOpenGL2-9.2.so.1(+0x1d29b9)
7 lib/libvtkRenderingOpenGL2-9.2.so.1(+0x1d2924)
6 glXChooseFBConfig + 35

```

My guess would be an invalid `DisplayId` when `glXChooseFBConfig()` is called.

---

<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: [November 12, 2022, 3:23pm UTC](https://discourse.vtk.org/t/seg-faults-on-render-call/9803/3 "2022-11-12T15:23:54Z")

</div>

The test program doesn’t crash if I revert [7ce7fd78f6](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/9026/diffs?commit_id=7ce7fd78f69208378e9a6182df249f9f464f87b3). I think the `Finalize()` method will need to be fixed, which is strange because it already clears the `DisplayId` like it should. Maybe there are other variables that it also needs to clear so that `CreateAWindow()` can safely be called. @mwestphal

Edit: I think I see the problem. The `DisplayId` is cleared in the interactor, but the `DisplayId` of the window is not cleared (dangling pointer).

---

<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: [November 12, 2022, 4:01pm UTC](https://discourse.vtk.org/t/seg-faults-on-render-call/9803/4 "2022-11-12T16:01:30Z")

</div>

I created the following patch which seems to fix the regression: [!9686](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/9686/diffs).

* * *

@marcomusy you can work around this problem by adding the following line after each `iren.Start()`:

```python
iren.GetRenderWindow().SetDisplayId("_0_p_void")

```

This will manually clear the `DisplayId` that VTK should have been clearing automatically.

---

<div class="post-metadata">

### Author: ![marcomusy](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marcomusy/32/95_2.png) [@marcomusy](https://discourse.vtk.org/u/marcomusy)
#### Post date: [November 13, 2022, 2:54pm UTC](https://discourse.vtk.org/t/seg-faults-on-render-call/9803/5 "2022-11-13T14:54:47Z")

</div>

Thanks for the quick fix! It doesn’t crash anymore but still the second `Start()` is ingnored:

```python
import vtk

cone = vtk.vtkConeSource()

mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(cone.GetOutputPort())

actor = vtk.vtkActor()
actor.SetMapper(mapper)

ren = vtk.vtkRenderer()
ren.AddActor(actor)

renwin = vtk.vtkRenderWindow()
renwin.AddRenderer(ren)

iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renwin)

iren.Start()
iren.GetRenderWindow().SetDisplayId("_0_p_void")

iren.Render()
iren.Start() # ignored
iren.GetRenderWindow().SetDisplayId("_0_p_void")

```

---

<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: [November 13, 2022, 4:15pm UTC](https://discourse.vtk.org/t/seg-faults-on-render-call/9803/6 "2022-11-13T16:15:32Z")

</div>

The `vtkXRenderWindowInteractor::Finalize()` method apparently puts the interactor into an unusable state. This should be easy to fix in the VTK codebase.

In the meantime, I think that your only option is to create a new interactor:

```diff
 iren.Start()
 iren.GetRenderWindow().SetDisplayId("_0_p_void")

+iren = vtk.vtkRenderWindowInteractor()
+iren.SetRenderWindow(renwin)
 iren.Render()
 iren.Start()

```
