go-gtk icon indicating copy to clipboard operation
go-gtk copied to clipboard

Crash when trying to use signal callbacks with custom arguments

Open tsudoko opened this issue 9 years ago • 3 comments


func syncText(from, to *gtk.TextBuffer) {
    var start, end gtk.TextIter

    from.GetStartIter(&start)
    from.GetEndIter(&end)

    to.SetText(from.GetText(&start, &end, true))
}

func main() {
(...)
        leftBuf := leftTV.GetBuffer()
        rightBuf := rightTV.GetBuffer()
        leftBuf.Connect("changed", syncText, leftBuf, rightBuf)
        rightBuf.Connect("changed", syncText, rightBuf, leftBuf)
(...)
}

Full code here, you'll need to uncomment lines 52-57.

Crash after trying to enter text into one of TextViews:

panic: reflect: Call using zero Value argument

goroutine 1 [running]:
reflect.Value.call(0x5471e0, 0x608008, 0x13, 0x5b6e00, 0x4, 0xc20803a270, 0x2, 0x2, 0x0, 0x0, ...)
        /usr/lib/go/src/reflect/value.go:360 +0x491
reflect.Value.Call(0x5471e0, 0x608008, 0x13, 0xc20803a270, 0x2, 0x2, 0x0, 0x0, 0x0)
        /usr/lib/go/src/reflect/value.go:296 +0xbc
github.com/mattn/go-gtk/glib._go_glib_callback(0x1c6fad0)
        /home/flan/.local/share/go/src/github.com/mattn/go-gtk/glib/glib.go:708 +0x1b4
github.com/mattn/go-gtk/gtk._Cfunc_gtk_main()
        /home/flan/.local/share/go/src/github.com/mattn/go-gtk/gtk/:3629 +0x45
github.com/mattn/go-gtk/gtk.Main()
        /home/flan/.local/share/go/src/github.com/mattn/go-gtk/gtk/gtk.go:246 +0x1b
main.main()
        /home/flan/.src/font-compare/main.go:64 +0x979

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
        /usr/lib/go/src/runtime/asm_amd64.s:2232 +0x1

I'm pretty sure I'm just not using custom arguments in callbacks properly, but I couldn't find what's the right way to do it.

tsudoko avatar Sep 17 '15 08:09 tsudoko

signature of changed event should be:

http://www.gtk.org/api/2.6/gtk/GtkTextBuffer.html#GtkTextBuffer-changed

mattn avatar Sep 17 '15 11:09 mattn

Wouldn't changing leftBuf.Connect("changed", syncText, leftBuf, rightBuf) to leftBuf.Connect("changed", syncText, rightBuf) fix it, then? I tried that and changed syncText() to accept interface{} as a second argument to match the function prototype from the link, but it still crashes.

Modified syncText():

func syncText(from *gtk.TextBuffer, user_data interface{}) {
    var start, end gtk.TextIter
    to := user_data.(gtk.TextBuffer)

    from.GetStartIter(&start)
    from.GetEndIter(&end)
    print(from.GetText(&start, &end, true))

    to.SetText(from.GetText(&start, &end, true))
}

Or do I need to actually use the C type gpointer directly?

tsudoko avatar Sep 17 '15 11:09 tsudoko

Ah, sorry. See example code here.

https://github.com/mattn/go-gtk/blob/master/example/event/event.go#L24-L27

You can get argument via glib.CallbackContext like calling ctx.Args(0). first argument should be GtkTextBuffer*, and second one should be gpointer.

http://www.gtk.org/api/2.6/gtk/GtkTextBuffer.html#GtkTextBuffer-changed

mattn avatar Sep 17 '15 12:09 mattn