Julia Gtk segmentation violation when changing a GtkScale widget

This works for me, the main difference is that I pass scale as an argument instead of keeping it global (I recommend avoiding globals as much as possible). I also use the “full syntax” for signal_connect, which is a bit more complicated but more robust in my experience, see this for more details. The @guarded part is actually very useful, without it any errors in your callbacks will crash Gtk. Maybe there’s a way to do with the “simple” signal_connect, I’m not sure.

Read this also.

resetscale = true # set this false to avoid the crash
using Gtk
c = @GtkCanvas(200, 100)
win = GtkWindow("Crash demo")
g = GtkGrid()
push!(win, g)
scale = GtkScale(false, 0:50.)
cb = GtkComboBoxText()
push!(cb, "one")
push!(cb, "two")
set_gtk_property!(cb,:active,1)

function selectionchanged(index)
    println("selection changed to index $index")
end

@guarded (nothing) function cbchanged(widget, user_data)
    v = 30.
    scale = user_data
    idx = get_gtk_property(cb, :active, Int)
    str = Gtk.bytestring( GAccessor.active_text(cb) )
    selectionchanged(idx + 1) # change from zero- to 1-base
    if resetscale
        println("Prepare to set scale value to $v")
        Gtk.GAccessor.value(scale, v)
    end
    nothing
end

id = signal_connect(cbchanged, cb, "changed", Nothing, (), false, scale)

function scalechanged(widget)
    value = Gtk.GAccessor.value(widget)
    println("scale changed to $value")
    nothing
end
id = signal_connect(scalechanged, scale, "value_changed")
v = 45.

if resetscale   
    println("Prepare to set scale value to $v")
    Gtk.GAccessor.value(scale, v)
    println("Successfully set scale value to $v")
end
g[1:6,1] = c
g[2:5,2] = scale
g[1,2] = cb
showall(win)