Julia Gtk segmentation violation when changing a GtkScale widget

I’d appreciate any help in solving the following problem:

Julia 1.0.1 Gtk crashes when changing a GtkScale under MacOS 10.14 and Windows 7. It sounds like a stack management error. The simplest example I could make (extracted from a much more complex script) is this:

Julia Gtk script cbcrashdemo.jl crashes with a seg fault when changing a GtkScale value

There is no crash in MacOS when this is not done in a signal callback function

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

function cbchanged(widget)
v = 30.
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”)

THE FOLLOWING STATEMENT CAUSES A CRASH UNDER MACOS AND WINDOWS IF RESETSCALE IS TRUE

#= This is the MacOS error message; Windows 7 is similar but not identical
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: EXC_I386_GPFLT
Exception Note: EXC_CORPSE_NOTIFY

Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL, Code 0xb
Terminating Process: exc handler [2877]

=#
Gtk.GAccessor.value(scale, v)
end
end
id = signal_connect(cbchanged, cb, “changed”)
function scalechanged(widget)
value = Gtk.GAccessor.value(widget)
println(“scale changed to $value”)
end
id = signal_connect(scalechanged, scale, “value_changed”)

The following crashes under Windows 7 but not under MacOS

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)

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)

Thanks, also for the link to the extended form of signal_connect, which I hadn’t found. I take your point about the @guarded try and catch macro, but in my course examples I try to prevent exceptions from occurring (e.g., don’t divide by zero). However, that wouldn’t work for problems in Gtk stack management or thread switching. Your fix works in Windows 7 also, although the Julia REPL in my installation of that OS is defective-- the ; shell won’t do cd or dir, and the path separator is / as in Unix instead of \ as in Windows. But perhaps that’s off point here (and it could be better in a newer Windows that I can’t test). Thanks again for getting my project over a brick wall!