mattn / go-gtk

Go binding for GTK
http://mattn.github.com/go-gtk
BSD 3-Clause "New" or "Revised" License
2.12k stars 246 forks source link

Go devel and pointers problem #291

Closed pztrn closed 7 years ago

pztrn commented 7 years ago

Just ran go run -race and:

panic: runtime error: cgo argument has Go pointer to Go pointer

goroutine 1 [running, locked to thread]:
panic(0x44f46c0, 0xc4203b1fe0)
        /usr/local/Cellar/go/1.7.3/libexec/src/runtime/panic.go:500 +0x1ae
github.com/mattn/go-gtk/gtk.(*TreeModel).GetValue(0xc4204c4010, 0xc4202fc740, 0x2, 0xc4202fc780)
        /Volumes/DATA/.go/src/github.com/mattn/go-gtk/gtk/gtk.go:4189 +0x17e
github.com/pztrn/urtrator/ui.(*MainWindow).sortServersByName(0xc42008c000, 0xc4204c4010, 0xc4202fc720, 0xc4202fc740, 0x44fbfa0)
        /Volumes/DATA/.go/src/github.com/pztrn/urtrator/ui/mainwindow_servers_sorting.go:20 +0x775
github.com/pztrn/urtrator/ui.(*MainWindow).(github.com/pztrn/urtrator/ui.sortServersByName)-fm(0xc4204c4010, 0xc4202fc720, 0xc4202fc740, 0xc4200b8048)
        /Volumes/DATA/.go/src/github.com/pztrn/urtrator/ui/mainwindow_init.go:446 +0x60
github.com/mattn/go-gtk/gtk._go_call_sort_func(0x6337140)
        /Volumes/DATA/.go/src/github.com/mattn/go-gtk/gtk/gtk.go:4888 +0x368
github.com/mattn/go-gtk/gtk._cgoexpwrap_7d7d16b2fc6c__go_call_sort_func(0x6337140)
        ??:0 +0x39
github.com/mattn/go-gtk/gtk._Cfunc__gtk_list_store_set_ptr(0x70a8c90, 0xc4202fc680, 0x0, 0x8872000)
        ??:0 +0x63
github.com/mattn/go-gtk/gtk.(*ListStore).SetValue(0xc420458980, 0xc4202fc680, 0x0, 0x44c9a40, 0x8872000)
        /Volumes/DATA/.go/src/github.com/mattn/go-gtk/gtk/gtk.go:5134 +0xd6f
github.com/pztrn/urtrator/ui.(*MainWindow).loadAllServers(0xc42008c000, 0xc4203ae270)
        /Volumes/DATA/.go/src/github.com/pztrn/urtrator/ui/mainwindow.go:358 +0x12eb
github.com/pztrn/urtrator/ui.(*MainWindow).(github.com/pztrn/urtrator/ui.loadAllServers)-fm(0xc4203ae270)
        /Volumes/DATA/.go/src/github.com/pztrn/urtrator/ui/mainwindow_init.go:174 +0x4c
github.com/pztrn/urtrator/eventer.(*Eventer).reallyLaunchEvent(0xc42002a070, 0x454d1f6, 0xe, 0xc4203ae270)
        /Volumes/DATA/.go/src/github.com/pztrn/urtrator/eventer/eventer_object.go:64 +0x245
github.com/pztrn/urtrator/eventer.(*Eventer).LaunchEvent.func1(0x0)
        /Volumes/DATA/.go/src/github.com/pztrn/urtrator/eventer/eventer_object.go:54 +0x5d
reflect.Value.call(0x44d7f40, 0xc4203ae2a0, 0x13, 0x4547afe, 0x4, 0x47ba3c8, 0x0, 0x0, 0x47ba3c8, 0x10000c420014208, ...)
        /usr/local/Cellar/go/1.7.3/libexec/src/reflect/value.go:434 +0x6d6
reflect.Value.Call(0x44d7f40, 0xc4203ae2a0, 0x13, 0x47ba3c8, 0x0, 0x0, 0xc4202fc600, 0x1, 0x1)
        /usr/local/Cellar/go/1.7.3/libexec/src/reflect/value.go:302 +0xc1
github.com/mattn/go-gtk/glib._go_glib_sourcefunc(0x7b47dc0)
        /Volumes/DATA/.go/src/github.com/mattn/go-gtk/glib/glib.go:909 +0x1e4
github.com/mattn/go-gtk/glib._cgoexpwrap_b00a5877f6a0__go_glib_sourcefunc(0x7b47dc0)
        ??:0 +0x39
github.com/mattn/go-gtk/gtk._Cfunc_gtk_main()
        ??:0 +0x5f
github.com/mattn/go-gtk/gtk.Main()
        /Volumes/DATA/.go/src/github.com/mattn/go-gtk/gtk/gtk.go:248 +0x30
github.com/pztrn/urtrator/ui.(*MainWindow).Initialize(0xc42008c000)
        /Volumes/DATA/.go/src/github.com/pztrn/urtrator/ui/mainwindow_init.go:168 +0x17a0
main.main()
        /Volumes/DATA/Projects/github/my/urtrator/urtrator.go:33 +0x248
exit status 2

Is that normal?

pztrn commented 7 years ago

Tested on go devel - with go run -race message is still here.

mattn commented 7 years ago

go-gtk doesn't care of race.

pztrn commented 7 years ago

Ok, so I'm closing then, thanks for clarification.

pztrn commented 7 years ago

Sorry, I'm reopening, with new additions to original bugreport: now it happens on Windows only, even with go devel:

panic: runtime error: cgo argument has Go pointer to Go pointer

goroutine 1 [running, locked to thread]:
panic(0x69fa60, 0xc0422a0ad0)
        C:/msys64/home/pztrn/go-git/src/runtime/panic.go:531 +0x1dd
github.com/mattn/go-gtk/gtk.(*TreeModel).GetValue.func1(0xf2eb30, 0xc042441700, 0x2, 0xc042441740)
        github.com/mattn/go-gtk/gtk/_obj/gtk.cgo1.go:7832 +0xeb
github.com/mattn/go-gtk/gtk.(*TreeModel).GetValue(0xc042004028, 0xc042441700, 0x2, 0xc042441740)
        github.com/mattn/go-gtk/gtk/_obj/gtk.cgo1.go:7841 +0x64
github.com/pztrn/urtrator/ui.(*MainWindow).sortServersByName(0xc0420846e0, 0xc042004028, 0xc042441700, 0xc042441720, 0xc042441720)
        C:/msys64/home/pztrn/GOPATH/src/github.com/pztrn/urtrator/ui/mainwindow_servers_sorting.go:19 +0x3ff
github.com/pztrn/urtrator/ui.(*MainWindow).(github.com/pztrn/urtrator/ui.sortServersByName)-fm(0xc042004028, 0xc042441700, 0xc042441720, 0xc0424e8048)
        C:/msys64/home/pztrn/GOPATH/src/github.com/pztrn/urtrator/ui/mainwindow_init.go:448 +0x4f
github.com/mattn/go-gtk/gtk._go_call_sort_func(0x47205f0)
        github.com/mattn/go-gtk/gtk/_obj/gtk.cgo1.go:9023 +0x1b8
github.com/mattn/go-gtk/gtk._cgoexpwrap_7e57c1b41650__go_call_sort_func(0x47205f0)
        github.com/mattn/go-gtk/gtk/_obj/_cgo_gotypes.go:21625 +0x32
github.com/mattn/go-gtk/gtk._Cfunc__gtk_list_store_set_ptr(0xf2eb30, 0xc0424416e0, 0x0, 0xf20ea0)
        github.com/mattn/go-gtk/gtk/_obj/_cgo_gotypes.go:2872 +0x4c
github.com/mattn/go-gtk/gtk.(*ListStore).SetValue.func4(0xf2eb30, 0xc0424416e0, 0x0, 0xf20ea0)
        github.com/mattn/go-gtk/gtk/_obj/gtk.cgo1.go:9324 +0x14e
github.com/mattn/go-gtk/gtk.(*ListStore).SetValue(0xc04200f790, 0xc0424416e0, 0x0, 0x674f00, 0xf20ea0)
        github.com/mattn/go-gtk/gtk/_obj/gtk.cgo1.go:9326 +0x1d9
github.com/pztrn/urtrator/ui.(*MainWindow).loadAllServers(0xc0420846e0, 0xc04217cd50)
        C:/msys64/home/pztrn/GOPATH/src/github.com/pztrn/urtrator/ui/mainwindow.go:358 +0xb52
github.com/pztrn/urtrator/ui.(*MainWindow).(github.com/pztrn/urtrator/ui.loadAllServers)-fm(0xc04217cd50)
        C:/msys64/home/pztrn/GOPATH/src/github.com/pztrn/urtrator/ui/mainwindow_init.go:174 +0x3b
github.com/pztrn/urtrator/eventer.(*Eventer).reallyLaunchEvent(0xc042004080, 0x6fafa7, 0xe, 0xc04217cd50)
        C:/msys64/home/pztrn/GOPATH/src/github.com/pztrn/urtrator/eventer/eventer_object.go:64 +0x19e
github.com/pztrn/urtrator/eventer.(*Eventer).LaunchEvent.func1(0x0)
        C:/msys64/home/pztrn/GOPATH/src/github.com/pztrn/urtrator/eventer/eventer_object.go:54 +0x4c
reflect.Value.call(0x683260, 0xc04217cd80, 0x13, 0x6f55be, 0x4, 0xa3b2b0, 0x0, 0x0, 0xc04217cdb0, 0x675040, ...)
        C:/msys64/home/pztrn/go-git/src/reflect/value.go:434 +0x926
reflect.Value.Call(0x683260, 0xc04217cd80, 0x13, 0xa3b2b0, 0x0, 0x0, 0x3, 0xc04201e000, 0xc04200c300)
        C:/msys64/home/pztrn/go-git/src/reflect/value.go:302 +0xab
github.com/mattn/go-gtk/glib._go_glib_sourcefunc(0x47e2580)
        github.com/mattn/go-gtk/glib/_obj/glib.cgo1.go:1690 +0x134
github.com/mattn/go-gtk/glib._cgoexpwrap_08b8c9044f35__go_glib_sourcefunc(0x47e2580)
        github.com/mattn/go-gtk/glib/_obj/_cgo_gotypes.go:1849 +0x32
github.com/mattn/go-gtk/gtk._Cfunc_gtk_main()
        github.com/mattn/go-gtk/gtk/_obj/_cgo_gotypes.go:10359 +0x48
github.com/mattn/go-gtk/gtk.Main()
        github.com/mattn/go-gtk/gtk/_obj/gtk.cgo1.go:1041 +0x1b
github.com/pztrn/urtrator/ui.(*MainWindow).Initialize(0xc0420846e0)
        C:/msys64/home/pztrn/GOPATH/src/github.com/pztrn/urtrator/ui/mainwindow_init.go:168 +0xe82

yet macOS and Linux builds fine with go devel.

This can be repro with latest git for https://github.com/pztrn/urtrator

mattn commented 7 years ago

Could you please make minimal example to reproduce?

pztrn commented 7 years ago

Of course.

package main

import (
    "strconv"

    "github.com/mattn/go-gtk/glib"
    "github.com/mattn/go-gtk/gtk"
)

var (
    mainwindow *gtk.Window
    vbox *gtk.VBox

    treeview *gtk.TreeView
    treeview_store *gtk.ListStore
    treeview_store_sortable *gtk.TreeSortable
)

func close() {
    gtk.MainQuit()
}

func sortFunction(model *gtk.TreeModel, a *gtk.TreeIter, b *gtk.TreeIter, col int) int {
    var item1_raw glib.GValue
    var item2_raw glib.GValue

    model.GetValue(a, col, &item1_raw)
    model.GetValue(b, col, &item2_raw)

    item1, _ := strconv.Atoi(item1_raw.GetString())
    item2, _ := strconv.Atoi(item2_raw.GetString())

    if item1 < item2 {
        return 1
    } else {
        return -1
    }

    return 0
}

func sortFunc1(model *gtk.TreeModel, a *gtk.TreeIter, b *gtk.TreeIter) int {
    return sortFunction(model, a, b, 0)
}

func sortFunc2(model *gtk.TreeModel, a *gtk.TreeIter, b *gtk.TreeIter) int {
    return sortFunction(model, a, b, 1)
}

func sortFunc3(model *gtk.TreeModel, a *gtk.TreeIter, b *gtk.TreeIter) int {
    return sortFunction(model, a, b, 2)
}

func main() {
    gtk.Init(nil)

    mainwindow := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
    mainwindow.SetTitle("TreeView sorting test")
    mainwindow.Connect("destroy", close)
    mainwindow.SetDefaultSize(500, 500)

    vbox := gtk.NewVBox(false, 0)

    treeview := gtk.NewTreeView()
    treeview_store := gtk.NewListStore(glib.G_TYPE_STRING, glib.G_TYPE_STRING, glib.G_TYPE_STRING)
    treeview_store_sortable := gtk.NewTreeSortable(treeview_store)
    treeview.SetModel(treeview_store)

    col1 := gtk.NewTreeViewColumnWithAttributes("1", gtk.NewCellRendererText(), "markup", 0)
    col1.SetSortColumnId(0)
    treeview.AppendColumn(col1)

    col2 := gtk.NewTreeViewColumnWithAttributes("2", gtk.NewCellRendererText(), "markup", 1)
    col2.SetSortColumnId(1)
    treeview.AppendColumn(col2)

    col3 := gtk.NewTreeViewColumnWithAttributes("3", gtk.NewCellRendererText(), "markup", 2)
    col3.SetSortColumnId(2)
    treeview.AppendColumn(col3)

    treeview_store_sortable.SetSortColumnId(0, gtk.SORT_ASCENDING)
    treeview_store_sortable.SetSortFunc(0, sortFunc1)
    treeview_store_sortable.SetSortFunc(1, sortFunc2)
    treeview_store_sortable.SetSortFunc(2, sortFunc3)

    swin1 := gtk.NewScrolledWindow(nil, nil)
    swin1.Add(treeview)

    vbox.PackStart(swin1, true, true, 0)

    mainwindow.Add(vbox)

    mainwindow.ShowAll()

    for i := 0; i < 10000; i++ {
        iter := new(gtk.TreeIter)
        treeview_store.Append(iter)
        treeview_store.SetValue(iter, 0, strconv.Itoa(i))
        treeview_store.SetValue(iter, 1, strconv.Itoa(i + 100 / 20))
        treeview_store.SetValue(iter, 2, strconv.Itoa(i + 200 / 10))
    }

    gtk.Main()
}

Work fine on Linux and macOS, on windows it's crashing with following traceback:

panic: runtime error: cgo argument has Go pointer to Go pointer

goroutine 1 [running]:
panic(0x5377a0, 0xc04200e3d0)
        C:/msys64/home/pztrn/go-git/src/runtime/panic.go:531 +0x1dd
github.com/mattn/go-gtk/gtk.(*TreeModel).GetValue.func1(0x86ed30, 0xc042002980, 0x0, 0xc0420029c0)
        github.com/mattn/go-gtk/gtk/_obj/gtk.cgo1.go:7832 +0xeb
github.com/mattn/go-gtk/gtk.(*TreeModel).GetValue(0xc042004080, 0xc042002980, 0x0, 0xc0420029c0)
        github.com/mattn/go-gtk/gtk/_obj/gtk.cgo1.go:7841 +0x64
main.sortFunction(0xc042004080, 0xc042002960, 0xc042002980, 0x0, 0xc042002980)
        C:/msys64/home/pztrn/1.go:28 +0xb4
main.sortFunc1(0xc042004080, 0xc042002960, 0xc042002980, 0xc042052048)
        C:/msys64/home/pztrn/1.go:43 +0x4f
github.com/mattn/go-gtk/gtk._go_call_sort_func(0x38bb700)
        github.com/mattn/go-gtk/gtk/_obj/gtk.cgo1.go:9023 +0x1b8
github.com/mattn/go-gtk/gtk._cgoexpwrap_7e57c1b41650__go_call_sort_func(0x38bb700)
        github.com/mattn/go-gtk/gtk/_obj/_cgo_gotypes.go:21625 +0x32
github.com/mattn/go-gtk/gtk._Cfunc_gtk_list_store_set_value(0x86ed30, 0xc042002940, 0xc000000000, 0x38fe710)
        github.com/mattn/go-gtk/gtk/_obj/_cgo_gotypes.go:10328 +0x4c
github.com/mattn/go-gtk/gtk.(*ListStore).SetValue.func1(0x86ed30, 0xc042002940, 0x0, 0x38fe710)
        github.com/mattn/go-gtk/gtk/_obj/gtk.cgo1.go:9291 +0x115
github.com/mattn/go-gtk/gtk.(*ListStore).SetValue(0xc04200e250, 0xc042002940, 0x0, 0x52a720, 0xc04200e360)
        github.com/mattn/go-gtk/gtk/_obj/gtk.cgo1.go:9299 +0x227
main.main()
        C:/msys64/home/pztrn/1.go:98 +0x5ca
exit status 2
pztrn commented 7 years ago

Done some testing with my example - all works if I comment this line:

treeview_store_sortable.SetSortColumnId(0, gtk.SORT_ASCENDING)
pztrn commented 7 years ago

Yep, confirming, it works :) Thanks!