# Avoiding segfault on exit when using QML.jl

**URL:** https://discourse.julialang.org/t/avoiding-segfault-on-exit-when-using-qml-jl/128737
**Category:** General Usage
**Tags:** question, segfault, qml
**Created:** [May 6, 2025, 12:10am UTC](https://discourse.julialang.org/t/avoiding-segfault-on-exit-when-using-qml-jl/128737 "2025-05-06T00:10:01Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Tetrakai](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@Tetrakai](https://discourse.julialang.org/u/Tetrakai)
#### Post date: [May 6, 2025, 12:10am UTC](https://discourse.julialang.org/t/avoiding-segfault-on-exit-when-using-qml-jl/128737/1 "2025-05-06T00:10:01Z")

</div>

My real code runs great, but for some reason gives a segfault when exiting the Julia REPL. Then I saw `JuliaPropertyMaps` should be a function in the [docs](https://juliagraphics.github.io/QML.jl/dev/#Using-QML.jl-inside-another-Julia-module) .

I modified the doc example to show my problem (I need “Hi” to become “Hello”):

```julia
using QML

# Propertymap is a pointer, so it needs to be a function and not a const value
props() = JuliaPropertyMap(
    "hi" => "Hi",
)

world() = " world!"

# Convenience function to call all relevant function macros
function define_funcs()
  @qmlfunction world
end

function main(qml_file)
  define_funcs()
  loadqml(qml_file; props=props())
  exec()
end

mktempdir() do folder
            path = joinpath(folder, "frommodule.qml")
            write(path, """
                  import QtQuick
                  import QtQuick.Controls
                  import org.julialang
                  
                  ApplicationWindow {
                    id: mainWin
                    title: "My Application"
                    width: 400
                    height: 400
                    visible: true
                  
                    Rectangle {
                      anchors.fill: parent
                      color: "red"
                  
                      Button {
                        anchors.centerIn: parent
                        text: props.hi + Julia.world()
                        onClicked: { props.hi = "Hello"; console.log("Value:", props.hi) }
                      }
                    }
                  }            
                  """)
            main(path)

          end

props()

```

Results:

```julia
Qt Debug: Value: Hello (file:///tmp/jl_Lq0Tc5/frommodule.qml:19, expression for onClicked)

julia> props()
JuliaPropertyMap with 1 entry:
  "hi" => "Hi"

```

If I use this method to avoid the segfault (not sure yet if that will work for this case), how can I pass the new value to Julia?

**Bonus**  
Here is the error I am trying to avoid on exiting Julia:

```julia
[2093301] signal 11 (1): Segmentation fault
in expression starting at none:0
unknown function (ip: 0x20)
_ZN5jlcxx6detail11CallFunctorIvJPN7qmlwrap14JuliaItemModelEEE5applyEPKvNS_13WrappedCppPtrE at /home/user/.julia/artifacts/7bc654121da55f7ea29f116c66155e8e5ebb43ff/lib/libjlqml.so (unknown line)
Allocations: 42114717 (Pool: 42113248; Big: 1469); GC: 25
Segmentation fault (core dumped)

```

The relevant code to the segfault looks like the below. Not a MWE unfortunately.

Julia:

```julia
mutable struct Ivals
    name :: String
    idx :: Int
    sel :: Bool
end

function update_params(dat)
    params["ivals"] = new_ivals
end

@qmlfunction load_data
@qmlfunction update_params

ivals = JuliaItemModel(Ivals.(names(dat)[ivals0], ivals0, false))
params = JuliaPropertyMap("ivals" = ivals)

loadqml(qmlfile, params = params)

```

QML:

```julia
params.dat = Julia.load_data(params.fpath)
Julia.update_params(params.dat)

```

**Additional info**  
Say instead I try to assign the new values from the QML (which works for other values that are not `JuliaItemModel`):

Julia:

```julia
function update_params(dat)
    return new_ivals
end

```

QML:

```julia
params.ivals = Julia.update_params(params.dat)

```

Before `ivals` is updated:

```julia
println(ivals)
QML.JuliaItemModelAllocated(Ptr{Nothing} @0x00000000035fee70)

println(propertynames(ivals))
(:cpp_object,)

```

After update:

```julia
println(ivals)
QML.QObjectDereferenced(Ptr{Nothing} @0x00000000058be580)

println(propertynames(ivals))
(:cpp_object,)

```

Then I start seeing a different error to track down:

```julia
MethodError: no method matching length(::QML.QObjectDereferenced)

```

Maybe that is the easiest fix? Any ideas?

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [May 6, 2025, 12:41am UTC](https://discourse.julialang.org/t/avoiding-segfault-on-exit-when-using-qml-jl/128737/2 "2025-05-06T00:41:02Z")

</div>

What is the output of `versioninfo()`?

And what is the output of

```julia
using Pkg
Pkg.status()

```

?

---

<div class="post-metadata">

### Author: ![Tetrakai](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@Tetrakai](https://discourse.julialang.org/u/Tetrakai)
#### Post date: [May 6, 2025, 12:45am UTC](https://discourse.julialang.org/t/avoiding-segfault-on-exit-when-using-qml-jl/128737/3 "2025-05-06T00:45:00Z")

</div>

```julia
julia> versioninfo()
Julia Version 1.11.5
Commit 760b2e5b739 (2025-04-14 06:53 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 4 × Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, skylake)
Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)

julia> Pkg.status()
  [336ed68f] CSV v0.10.15
  [1f15a43c] CxxWrap v0.16.2
  [3b531cbf] DataConvenience v0.3.6
  [a93c6f00] DataFrames v1.7.0
  [c601a237] Interact v0.10.5
  [510215fc] Observables v0.5.5
  [d96e819e] Parameters v0.12.3
  [2db162a6] QML v0.9.2
  [2913bbd2] StatsBase v0.34.4
  [f3b207a7] StatsPlots v0.15.7

```

I had the same segfault on my Linux AMD Threadripper as well.

---

<div class="post-metadata">

### Author: ![Tetrakai](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@Tetrakai](https://discourse.julialang.org/u/Tetrakai)
#### Post date: [May 6, 2025, 5:12pm UTC](https://discourse.julialang.org/t/avoiding-segfault-on-exit-when-using-qml-jl/128737/4 "2025-05-06T17:12:12Z")

</div>

Would you like me to work out a MWE or anything?

I thought this was already known behavior based on the docs.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [May 6, 2025, 5:50pm UTC](https://discourse.julialang.org/t/avoiding-segfault-on-exit-when-using-qml-jl/128737/5 "2025-05-06T17:50:31Z")

</div>

Yes, indeed, it would be nice to have an MWE.

---

<div class="post-metadata">

### Author: ![Tetrakai](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@Tetrakai](https://discourse.julialang.org/u/Tetrakai)
#### Post date: [May 6, 2025, 6:54pm UTC](https://discourse.julialang.org/t/avoiding-segfault-on-exit-when-using-qml-jl/128737/7 "2025-05-06T18:54:30Z")

</div>

Ok… now it is actually using the temp folder. Run this, click the button, close the Qt window, then exit julia. You should see the segfault.

MWE:

```julia
using QML

# const qml_file = joinpath(pwd(), "code", "qml", "test.qml")

function update_props()
    props["fruits"] = fruits2
end

function main(qml_file, props)
    loadqml(qml_file, props = props)
    exec()
end

mutable struct Fruit
    name::String
    cost::Float64
end

fruits = JuliaItemModel([Fruit("apple", 1.0), Fruit("orange", 2.0)])
fruits2 = JuliaItemModel([Fruit("banana", 1.0), Fruit("pear", 2.0)])

props = JuliaPropertyMap("fruits" => fruits)

@qmlfunction update_props

# main(qml_file, props)

mktempdir() do folder
            path = joinpath(folder, "test.qml")
            write(path, """
                  import QtQuick
                  import QtQuick.Controls
                  import QtQuick.Layouts
                  import org.julialang

                  ApplicationWindow {
                      id: mainWin
                      title: "My Application"
                      width: 400
                      height: 400
                      visible: true
                    
                      ColumnLayout{
                        anchors.fill: parent
                    
                          Button {
                              height: 200
                              Layout.fillWidth: true
                              text: "Update Fruit"
                              onClicked: { 
                                  Julia.update_props()
                                  console.log("Value:", props.fruits) 
                              }
                         }
                          ListView {
                              model: props.fruits
                              Layout.fillHeight: true
                              Layout.fillWidth: true
                              delegate: Row {
                                  Text {
                                      text: name
                                  }
                              }
                          }
                      }
                  }
                  """)
            main(path, props)
          end

```

Error:

```julia
[2151785] signal 11 (1): Segmentation fault
in expression starting at none:0
unknown function (ip: 0x20)
_ZN5jlcxx6detail11CallFunctorIvJPN7qmlwrap14JuliaItemModelEEE5applyEPKvNS_13WrappedCppPtrE at /home/user/.julia/artifacts/7bc654121da55f7ea29f116c66155e8e5ebb43ff/lib/libjlqml.so (unknown line)
Allocations: 5190401 (Pool: 5189939; Big: 462); GC: 6
Segmentation fault (core dumped)

```

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [May 6, 2025, 7:05pm UTC](https://discourse.julialang.org/t/avoiding-segfault-on-exit-when-using-qml-jl/128737/8 "2025-05-06T19:05:49Z")

</div>

I can reproduce the issue, but only if I click on “update fruit” before closing the window and exiting Julia.

Where in the documentation did you find the indication that this would be to be expected?

---

<div class="post-metadata">

### Author: ![Tetrakai](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@Tetrakai](https://discourse.julialang.org/u/Tetrakai)
#### Post date: [May 6, 2025, 7:08pm UTC](https://discourse.julialang.org/t/avoiding-segfault-on-exit-when-using-qml-jl/128737/9 "2025-05-06T19:08:45Z")

</div>

Yes, it occurs when replacing a `JuliaItemModel`.

Not specifically this scenario, just guessed it was related to this:

> **[QML · QML.jl](https://juliagraphics.github.io/QML.jl/dev/#Using-QML.jl-inside-another-Julia-module)**
>
> Documentation for QML.jl.

---

<div class="post-metadata">

### Author: ![Tetrakai](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@Tetrakai](https://discourse.julialang.org/u/Tetrakai)
#### Post date: [May 6, 2025, 8:40pm UTC](https://discourse.julialang.org/t/avoiding-segfault-on-exit-when-using-qml-jl/128737/10 "2025-05-06T20:40:31Z")

</div>

I see the same error using a `Connections` block:

```julia
using QML

# const qml_file = joinpath(pwd(), "code", "qml", "test.qml")

function update_props()
    props["fruits"] = fruits2
end

function main(qml_file, props)
    loadqml(qml_file, props = props)
    exec()
end

mutable struct Fruit
    name::String
    cost::Float64
end

fruits = JuliaItemModel([Fruit("apple", 1.0), Fruit("orange", 2.0)])
fruits2 = JuliaItemModel([Fruit("banana", 1.0), Fruit("pear", 2.0)])

props = JuliaPropertyMap("fruits" => fruits)

@qmlfunction update_props

# main(qml_file, props)

mktempdir() do folder
            path = joinpath(folder, "test.qml")
            write(path, """
                  import QtQuick
                  import QtQuick.Controls
                  import QtQuick.Layouts
                  import org.julialang

                  ApplicationWindow {
                      id: mainWin
                      title: "My Application"
                      width: 400
                      height: 400
                      visible: true

                      Connections {
                        target: button
                        // onClicked: Julia.update_props()
                        function onClicked() { Julia.update_props() }
                      }
                    
                      ColumnLayout{
                        anchors.fill: parent
                    
                          Button {
                              id: button
                              height: 200
                              Layout.fillWidth: true
                              text: "Update Fruit"
                              // onClicked: { 
                              // Julia.update_props()
                              // console.log("Value:", props.fruits) 
                              // }
                         }
                          ListView {
                              model: props.fruits
                              Layout.fillHeight: true
                              Layout.fillWidth: true
                              delegate: Row {
                                  Text {
                                      text: name
                                  }
                              }
                          }
                      }
                  }
                  """)
            main(path, props)
          end

```

Also the syntax shown in the [readme](https://github.com/JuliaGraphics/QML.jl?tab=readme-ov-file#using-qtimer) is deprecated and returns a warning:

`QML Connections: Implicitly defined onFoo properties in Connections are deprecated. Use this syntax instead: function onFoo(<arguments>)`

From the QML docs:

> When connecting to signals in QML, the usual way is to create an “on” handler that reacts when a signal is received  
> […]  
> **Note:** For backwards compatibility you can also specify the signal handlers without `function`, like you would specify them directly in the target object. This is not recommended. If you specify one signal handler this way, then all signal handlers specified as `function` in the same Connections object are ignored.

> **[Connections QML Type | Qt Qml | Qt 6.9.0](https://doc.qt.io/qt-6/qml-qtqml-connections.html)**
>
> Describes generalized connections to signals.

So it seems I was using it as expected after all, is there a workaround?

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [May 6, 2025, 9:26pm UTC](https://discourse.julialang.org/t/avoiding-segfault-on-exit-when-using-qml-jl/128737/11 "2025-05-06T21:26:04Z")

</div>

I suggest that you create an issue at: [GitHub · Where software is built](https://github.com/JuliaGraphics/QML.jl/issues)

My knowledge is somewhat limited, and other developers should comment on this question. Still not clear to me if:

- this is a bug
- or an error in the documentation
- or wrong usage of the package

---

<div class="post-metadata">

### Author: ![Tetrakai](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@Tetrakai](https://discourse.julialang.org/u/Tetrakai)
#### Post date: [May 6, 2025, 9:37pm UTC](https://discourse.julialang.org/t/avoiding-segfault-on-exit-when-using-qml-jl/128737/12 "2025-05-06T21:37:48Z")

</div>

Done: [Segfault when exiting Julia after replacing a JuliaItemModel · Issue #218 · JuliaGraphics/QML.jl · GitHub](https://github.com/JuliaGraphics/QML.jl/issues/218)

---

<div class="post-metadata">

### Author: ![Tetrakai](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@Tetrakai](https://discourse.julialang.org/u/Tetrakai)
#### Post date: [May 7, 2025, 11:02am UTC](https://discourse.julialang.org/t/avoiding-segfault-on-exit-when-using-qml-jl/128737/13 "2025-05-07T11:02:51Z")

</div>

This works. First clear the array in QML, then push to it from Julia:

```julia
using QML

# const qml_file = joinpath(pwd(), "code", "qml", "test.qml")

function update_props()
    props["fruits"] = fruits2
end

# First clear props in QML
function update_props()
    [push!(props["fruits"], fruits2[i]) for i in 1:length(fruits2)]
end

function main(qml_file, props)
    loadqml(qml_file, props = props)
    exec()
end

mutable struct Fruit
    name::String
    cost::Float64
end

fruits = JuliaItemModel([Fruit("apple", 1.0), Fruit("orange", 2.0)])
fruits2 = JuliaItemModel([Fruit("banana", 1.0), Fruit("pear", 2.0)])

props = JuliaPropertyMap("fruits" => fruits)

@qmlfunction update_props

# main(qml_file, props)

mktempdir() do folder
            path = joinpath(folder, "test.qml")
            write(path, """
                  import QtQuick
                  import QtQuick.Controls
                  import QtQuick.Layouts
                  import org.julialang

                  ApplicationWindow {
                      id: mainWin
                      title: "My Application"
                      width: 400
                      height: 400
                      visible: true
                    
                      ColumnLayout{
                        anchors.fill: parent
                    
                          Button {
                              height: 200
                              Layout.fillWidth: true
                              text: "Update Fruit"
                              onClicked: {
                                  // First clear then update from julia
                                  props.fruits.clear() 
                                  Julia.update_props()
                              }
                         }
                          ListView {
                              model: props.fruits
                              Layout.fillHeight: true
                              Layout.fillWidth: true
                              delegate: Row {
                                  Text {
                                      text: name
                                  }
                              }
                          }
                      }
                  }
                  """)
            main(path, props)
          end

```

Still there should be a warning instead of that segfault.
