andlabs / ui

Platform-native GUI library for Go.
Other
8.33k stars 652 forks source link

Why this not work? #386

Closed DeadNumbers closed 4 years ago

DeadNumbers commented 4 years ago
var (
    mainwin  *ui.Window
    itemsEnt = make([]*ui.Entry, 20)
    soldsEnt = make([]*ui.Entry, 20)
    condsEnt = make([]*ui.Entry, 20)
)
    for i := 0; i < 20; i++ {
        itemsEnt = append(itemsEnt, ui.NewEntry())
        soldsEnt = append(soldsEnt, ui.NewEntry())
        condsEnt = append(condsEnt, ui.NewEntry())
    }

    for indx := range itemsEnt {
        grid1.Append(itemsEnt[indx], 0, indx+1, 1, 1, true, ui.AlignFill, false, ui.AlignFill)
        grid1.Append(soldsEnt[indx], 1, indx+1, 1, 1, true, ui.AlignFill, false, ui.AlignFill)
        grid1.Append(condsEnt[indx], 2, indx+1, 1, 1, true, ui.AlignFill, false, ui.AlignFill)
    }
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x48ba85]

goroutine 1 [running, locked to thread]:
github.com/andlabs/ui.(*Entry).LibuiControl(0x0, 0xc00004bab0)
        <autogenerated>:1 +0x5
github.com/andlabs/ui.(*Grid).Append.func1(0xc000180100, 0x4ec560, 0x0, 0x0, 0x1, 0x1, 0x1, 0xc00004bb01, 0x0, 0x16a1100, ...)
        /home/user/Go/pkg/mod/github.com/andlabs/ui@v0.0.0-20200610043537-70a69d6ae31e/grid.go:66 +0x43
github.com/andlabs/ui.(*Grid).Append(0xc000180100, 0x4ec560, 0x0, 0x0, 0x1, 0x1, 0x1, 0x1, 0x0, 0x467b00, ...)
        /home/user/Go/pkg/mod/github.com/andlabs/ui@v0.0.0-20200610043537-70a69d6ae31e/grid.go:70 +0xa9
main.amazon(0xc00005e040, 0x4ec801)
        /home/user/Go/src/orders/gui.go:46 +0x65c
main.setupUI()
        /home/user/Go/src/orders/gui.go:220 +0xea
github.com/andlabs/ui.pkguiDoQueueMain(0x0)
        /home/user/Go/pkg/mod/github.com/andlabs/ui@v0.0.0-20200610043537-70a69d6ae31e/main.go:103 +0xbd
github.com/andlabs/ui._cgoexpwrap_e99bb8362bf9_pkguiDoQueueMain(0x0)
        _cgo_gotypes.go:3968 +0x2b
github.com/andlabs/ui._Cfunc_uiMain()
        _cgo_gotypes.go:2520 +0x41
github.com/andlabs/ui.Main(0x4dab20, 0x0, 0xc000018178)
        /home/user/Go/pkg/mod/github.com/andlabs/ui@v0.0.0-20200610043537-70a69d6ae31e/main.go:41 +0xff
main.main()
        /home/user/Go/src/orders/gui.go:230 +0x2d
andlabs commented 4 years ago

Because you have a bug in your slice code. You create the slices with make([]*ui.Entry, 20), which creates a slice with 20 elements, all nil pointers. You then create the entries with itemsEnt = append(itemsEnt, ui.NewEntry()), which adds another item to the end of the slice, growing it from having length 20 to having length 21, with 20 nil pointers and 1 non-nil pointer. You wanted to say itemsEnt[i] = ui.NewEntry() instead. (Alternatively, you can use make([]*ui.Entry, 0, 20), which makes a slice that has length 0 but with space for 20 pointers pre-allocated. Then, your append() calls will work as you expect.)

DeadNumbers commented 4 years ago

Oh, I'm stupid and inattentive. @andlabs Thanks!