Skip to content

Crash on window close with vtkXRenderWindowInteractor

This is an issue in VTK 9.0 and 9.1 (and possibly earlier?)

The vtkXRenderWindowInteractor will crash when the window is closed, if the window called XOpenDisplay() before the interactor was attached to it. The problem is that, if the window thinks it owns the DisplayId handle (if win->OwnDisplay == 1), then closing the window will call XCloseDisplay(win->DisplayId), which leaves the iren->DisplayId dangling.

Conversely, if the interactor calls XOpenDisplay() then there is no crash when the window closes, but there is a resource leak since the interactor never calls XCloseDisplay(). See vtkXRenderWindowInteractor::Initialize():

  this->DisplayId = static_cast<Display*>(renWin->GetGenericDisplayId());
  if (!this->DisplayId)
  {
    vtkDebugMacro("opening display");
    this->DisplayId = XOpenDisplay(nullptr);
    vtkDebugMacro("opened display");
    renWin->SetDisplayId(this->DisplayId);
  }

Test code for reproducing the crash:

from vtkmodules.vtkRenderingCore import (
  vtkRenderer,
  vtkRenderWindow,
  vtkRenderWindowInteractor,
)
import vtkmodules.vtkRenderingUI
import vtkmodules.vtkRenderingOpenGL2
import vtkmodules.vtkInteractionStyle

ren = vtkRenderer()
ren.SetBackground(0.5, 0.2, 0.2)

renWin = vtkRenderWindow()
renWin.AddRenderer(ren)

# this line causes renWin->DisplayId and renWin->OwnDisplay to be set
renWin.Render()

iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# this line causes iren->DisplayId to be set to renWin->DisplayId
iren.Initialize()

# here we start the main loop, but when we close the window, renWin closes
# its DisplayId, and iren->DisplayId is left dangling (segfault!)
iren.Start()