# SegFault with in \`~qSlicerApplicationPrivate\` only with SlicerCAT

**URL:** <https://discourse.slicer.org/t/segfault-with-in-qslicerapplicationprivate-only-with-slicercat/22587>\
**Category:** Development\
**Created:** [March 18, 2022, 3:27pm UTC](https://discourse.slicer.org/t/segfault-with-in-qslicerapplicationprivate-only-with-slicercat/22587 "2022-03-18T15:27:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [March 18, 2022, 3:27pm UTC](https://discourse.slicer.org/t/segfault-with-in-qslicerapplicationprivate-only-with-slicercat/22587/1 "2022-03-18T15:27:37Z")

</div>

Hi,

When I updated the Slicer commit in my SlicerCAT I started to get Segmentation Fault in [qSlicerApplicationPrivate destructor](https://github.com/Slicer/Slicer/blob/5d334fd3885d0a717dbc7e38772edc5db8838ffb/Base/QTGUI/qSlicerApplication.cxx#L181)

This happens only with SlicerCAT (clean Slicer app works fine) even when all the modules are disabled ie SlicerCAT is launched with `--disable-modules`

Here are the steps that leads to this:

1. launch SlicerCAT
2. open `Application Settings`
3. close `Application Settings`
4. close SlicerCAT

It seems that `this->SettingsDialog` is already somehow unavailable by the time setting `setParent(nullptr)`  
I’ve tried to rebuild it, debug but without success. Even though the idea behind creating/deleting `SettingsDialog` is pretty clear.

If somebody uses SlicerCAT with recent commits (after 26 february) I would appreciate for testing that.

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [March 18, 2022, 3:56pm UTC](https://discourse.slicer.org/t/segfault-with-in-qslicerapplicationprivate-only-with-slicercat/22587/2 "2022-03-18T15:56:38Z")

</div>

Oh it seems I understand the root of a problem:

At first `Settings` has [no parent at the initilization time](https://github.com/Slicer/Slicer/blob/5d334fd3885d0a717dbc7e38772edc5db8838ffb/Base/QTGUI/qSlicerApplication.cxx#L274)

When we click on `Application Settings` the `Settings` object gets [`MainWindow` parent](https://github.com/Slicer/Slicer/blob/5d334fd3885d0a717dbc7e38772edc5db8838ffb/Base/QTGUI/qSlicerApplication.cxx#L745). That means `MainWindow` destructor will delete `Settings`.

Then in `~qSlicerApplicationPrivate()` destructor we are trying to set [`Settings` parent to `nullptr` and explicitely delete `Settings`](https://github.com/Slicer/Slicer/blob/5d334fd3885d0a717dbc7e38772edc5db8838ffb/Base/QTGUI/qSlicerApplication.cxx#L181-L182).

But what if `~MainWindow` destructor is called before `~qSlicerApplicationPrivate` destructor? Then in `~qSlicerApplicationPrivate` we will try to access deleted `Settings` object.

I added `cout` to these destructors to see who is deleted first and the output is:

```auto
$ ./Colada --disable-modules
QXcbConnection: XCB error: 3 (BadWindow), sequence: 1157, resource id: 14779307, major code: 40 (TranslateCoords), minor code: 0
MainWindow destructor is called
Switch to module: ""
qSlicerApplicationPrivate destructor is called
error: [~/Documents/Colada/d/Slicer-build/bin/./ColadaApp-real] exit abnormally - Report the problem.

```

That means in my SlicerCAT (I suspect in others too) `~MainWindow` is deleted first so there is no need to explicitely delete `Settings` in `~qSlicerApplicationPrivate` destructor.

Please take a look at this

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [March 19, 2022, 12:25am UTC](https://discourse.slicer.org/t/segfault-with-in-qslicerapplicationprivate-only-with-slicercat/22587/3 "2022-03-19T00:25:41Z")

</div>

The same issue is solved for the Python console, by using a `QPointer`. It would be great if you could submit a pull request that passes the ownership of the settings dialog similarly to how it is done for the Python console.

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [March 19, 2022, 12:44am UTC](https://discourse.slicer.org/t/segfault-with-in-qslicerapplicationprivate-only-with-slicercat/22587/4 "2022-03-19T00:44:31Z")

</div>

Could please show where can I find Python console `QPointer` solution?  
I have an idea how to solve this with `QPointer` but don’t know anything about python console implementation yet.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [March 19, 2022, 1:17am UTC](https://discourse.slicer.org/t/segfault-with-in-qslicerapplicationprivate-only-with-slicercat/22587/5 "2022-03-19T01:17:28Z")

</div>

Search for `QPointer` and `PythonConsole` in the source and header files of the Slicer application classes.

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [March 19, 2022, 1:23pm UTC](https://discourse.slicer.org/t/segfault-with-in-qslicerapplicationprivate-only-with-slicercat/22587/6 "2022-03-19T13:23:26Z")

</div>

The [PR is ready](https://github.com/Slicer/Slicer/pull/6275)
