# Julia Gtk segmentation violation when changing a GtkScale widget

**URL:** https://discourse.julialang.org/t/julia-gtk-segmentation-violation-when-changing-a-gtkscale-widget/16877
**Category:** New to Julia
**Created:** [October 27, 2018, 8:46pm UTC](https://discourse.julialang.org/t/julia-gtk-segmentation-violation-when-changing-a-gtkscale-widget/16877 "2018-10-27T20:46:23Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![rverdier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rverdier/32/9104_2.png) [@rverdier](https://discourse.julialang.org/u/rverdier)
#### Post date: [October 27, 2018, 8:46pm UTC](https://discourse.julialang.org/t/julia-gtk-segmentation-violation-when-changing-a-gtkscale-widget/16877/1 "2018-10-27T20:46:23Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)
#### Post date: [October 28, 2018, 10:13am UTC](https://discourse.julialang.org/t/julia-gtk-segmentation-violation-when-changing-a-gtkscale-widget/16877/2 "2018-10-28T10:13:50Z")

</div>

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](https://github.com/JuliaGraphics/Gtk.jl/blob/master/doc/more_signals.md) 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](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530) also.

```julia
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)

```

---

<div class="post-metadata">

### Author: ![rverdier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rverdier/32/9104_2.png) [@rverdier](https://discourse.julialang.org/u/rverdier)
#### Post date: [October 29, 2018, 3:10am UTC](https://discourse.julialang.org/t/julia-gtk-segmentation-violation-when-changing-a-gtkscale-widget/16877/3 "2018-10-29T03:10:11Z")

</div>

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!
