# Crash when using vtkQWidgetWidget

**URL:** https://discourse.vtk.org/t/crash-when-using-vtkqwidgetwidget/1387
**Category:** Support
**Created:** [July 19, 2019, 2:36pm UTC](https://discourse.vtk.org/t/crash-when-using-vtkqwidgetwidget/1387 "2019-07-19T14:36:39Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![cpinter](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cpinter/32/105_2.png) [@cpinter](https://discourse.vtk.org/u/cpinter)
#### Post date: [July 19, 2019, 2:36pm UTC](https://discourse.vtk.org/t/crash-when-using-vtkqwidgetwidget/1387/1 "2019-07-19T14:36:39Z")

</div>

Hi all,

I’m trying to use the relatively new class vtkQWidgetWidget in SlicerVR (to show a Qt widget in VR that can be manipulated using a laser pointer), and I keep getting a crash when rendering the scene after adding the prop to the renderer.

I checked that the VTK test TestQWidgetWidget works, and started from that code. When I changed the creation of the Qt widget on the stack to the heap, then it crashed.  
It seems that the crash happens on the first render after adding the prop. This is the log after the render call:  
`Python console user input: r.Render() Generic Warning: In C:\d\S4D\VTK\Rendering\OpenGL2\vtkOpenGLState.cxx, line 182 Error in cache state for GL_SCISSOR_BOX`  
Then it crashes. Not fully deterministically though: when I debug into the code and step slowly, it many times does not crash. Also in release mode sometimes it seems like crashing (few seconds wait), but then shows the widget as expected.

My first question is whether anybody actually used the vtkQWidgetWidget class in a real project, other than the test?  
Thanks!

---

<div class="post-metadata">

### Author: ![ken-martin](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ken-martin/32/884_2.png) [@ken-martin](https://discourse.vtk.org/u/ken-martin)
#### Post date: [July 22, 2019, 3:02pm UTC](https://discourse.vtk.org/t/crash-when-using-vtkqwidgetwidget/1387/2 "2019-07-22T15:02:07Z")

</div>

I use it in PV and it seems fine. The cache state error is I think still there (I forget if I fixed it or not) but shouldn’t be a real problem.

---

<div class="post-metadata">

### Author: ![cpinter](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cpinter/32/105_2.png) [@cpinter](https://discourse.vtk.org/u/cpinter)
#### Post date: [July 22, 2019, 4:45pm UTC](https://discourse.vtk.org/t/crash-when-using-vtkqwidgetwidget/1387/3 "2019-07-22T16:45:20Z")

</div>

Thanks! I’ll check out the PV code and see how it’s done there.

---

<div class="post-metadata">

### Author: ![cpinter](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cpinter/32/105_2.png) [@cpinter](https://discourse.vtk.org/u/cpinter)
#### Post date: [October 2, 2019, 8:41pm UTC](https://discourse.vtk.org/t/crash-when-using-vtkqwidgetwidget/1387/4 "2019-10-02T20:41:49Z")

</div>

The ParaView code that uses vtkQWidgetWidget seems equivalent to what I try to do, so unfortunately there are no clues there. A simple QWidget is created on the heap, and it’s set after the usual initialization calls to the widget.

It is quite strange though that both my code and the VTK test has the same issue: if the QWidget is created on the stack then they work, otherwise they crash.

If you replace  
`QPushButton hello( "Hello world!", 0 );`  
with  
`QPushButton* hello = new QPushButton( "Hello world!", 0 );`  
and  
`widget->SetWidget( &hello );`  
with  
`widget->SetWidget( hello );`  
then the crash should occur on the VTK test VTK::GUISupportQtCxx-TestQVTKOpenGLWidgetQWidgetWidget (TestQWidgetWidget.cxx).

Could somebody please confirm this? Thank you!

---

<div class="post-metadata">

### Author: ![ken-martin](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ken-martin/32/884_2.png) [@ken-martin](https://discourse.vtk.org/u/ken-martin)
#### Post date: [October 7, 2019, 7:57pm UTC](https://discourse.vtk.org/t/crash-when-using-vtkqwidgetwidget/1387/5 "2019-10-07T19:57:11Z")

</div>

I just checked that on master (windows nvidia qt 5.9.4) and it seemed fine. Which doesn’t mean everything is fine, just that my system seems to not show the issue. There have been some fixes to that class in the past month to address some rendering issues as well.

---

<div class="post-metadata">

### Author: ![cpinter](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cpinter/32/105_2.png) [@cpinter](https://discourse.vtk.org/u/cpinter)
#### Post date: [October 7, 2019, 8:08pm UTC](https://discourse.vtk.org/t/crash-when-using-vtkqwidgetwidget/1387/6 "2019-10-07T20:08:30Z")

</div>

Thanks for checking! I verified the same just this morning: the crash in the VTK test in debug mode doesn’t happen with the latest version. However, I still got the crash in SlicerVR (after cherry picking those commits on Slicer’s VTK). I’ve started clean builds also in release mode and will try to isolate the issue. I’ll let you know.

---

<div class="post-metadata">

### Author: ![cpinter](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cpinter/32/105_2.png) [@cpinter](https://discourse.vtk.org/u/cpinter)
#### Post date: [October 8, 2019, 5:04pm UTC](https://discourse.vtk.org/t/crash-when-using-vtkqwidgetwidget/1387/7 "2019-10-08T17:04:12Z")

</div>

Using the release build of the latest VTK, the QWidgetWidget tests seem OK even if instantiating the QWidget on the heap.

I cherry-picked two of your commits that were obviously related to this class, see the top two commits here: [https://github.com/cpinter/VTK/tree/slicervr-vtkqwidgetwidget-crash-fix](https://github.com/cpinter/VTK/tree/slicervr-vtkqwidgetwidget-crash-fix)  
(this is based on this branch [https://github.com/Slicer/VTK/tree/slicer-v8.2.0-2018-10-02-74d9488523](https://github.com/Slicer/VTK/tree/slicer-v8.2.0-2018-10-02-74d9488523), deviated from VTK proper October 2, 2018).

The crash I’m getting when I try to do the same thing that the TestQWidgetWidget does yields a call stack that only contains event loop calls, so it doesn’t tell much.

| Qt5Cored.dll!QEventDispatcherWin32::processEvents(QFlags flags) Line 646 | C++ |
| --- | --- |
| qwindowsd.dll!QWindowsGuiEventDispatcher::processEvents(QFlags flags) Line 74 | C++ |
| Qt5Cored.dll!QEventLoop::processEvents(QFlags flags) Line 135 | C++ |
| Qt5Cored.dll!QEventLoop::exec(QFlags flags) Line 212 | C++ |
| Qt5Cored.dll!QCoreApplication::exec() Line 1332 | C++ |
| Qt5Guid.dll!QGuiApplication::exec() Line 1688 | C++ |
| Qt5Widgetsd.dll!QApplication::exec() Line 2918 | C++ |
| qSlicerBaseQTCore.dll!qSlicerCoreApplication::exec() Line 775 | C++ |

Can you think of other commits that may be related to how the graphics resources are managed that may be related to this crash? I’d backport those too in case they fix my issue. Or if you have any other suggestions please let me know. Thank you!

---

<div class="post-metadata">

### Author: ![ken-martin](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ken-martin/32/884_2.png) [@ken-martin](https://discourse.vtk.org/u/ken-martin)
#### Post date: [October 8, 2019, 5:09pm UTC](https://discourse.vtk.org/t/crash-when-using-vtkqwidgetwidget/1387/8 "2019-10-08T17:09:36Z")

</div>

A year ago is a pretty big diff, lots of potential side effects. I know there was a big rework of the Qt interface in this MR [https://gitlab.kitware.com/vtk/vtk/merge\_requests/5214](https://gitlab.kitware.com/vtk/vtk/merge_requests/5214)
