fyne-io / fyne

Cross platform GUI toolkit in Go inspired by Material Design

Home Page:https://fyne.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

panic if app.New() is not the first method called.

williammoran opened this issue · comments

Checklist

  • I have searched the issue tracker for open issues that relate to the same problem, before opening a new one.
  • This issue only relates to a single bug. I will open new issues for any other problems.

Describe the bug

This code panics. I have no explanation for why it would panic. However, moving app.New() to the top of main() allows it to work. Anything to make this requirement clearer would be helpful.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0xa29ca9]

goroutine 7 [running]:
fyne.io/fyne/v2/app.(*settings).Theme(0x188c420?)
	/home/wmoran/go/pkg/mod/fyne.io/fyne/v2@v2.4.4/app/settings.go:66 +0x29
fyne.io/fyne/v2/theme.current()
	/home/wmoran/go/pkg/mod/fyne.io/fyne/v2@v2.4.4/theme/theme.go:179 +0x31
fyne.io/fyne/v2/theme.InnerPadding()
	/home/wmoran/go/pkg/mod/fyne.io/fyne/v2@v2.4.4/theme/size.go:91 +0x13
fyne.io/fyne/v2/widget.(*RichText).updateRowBounds(0xc000406000)
	/home/wmoran/go/pkg/mod/fyne.io/fyne/v2@v2.4.4/widget/richtext.go:362 +0x27
fyne.io/fyne/v2/widget.NewRichText(...)
	/home/wmoran/go/pkg/mod/fyne.io/fyne/v2@v2.4.4/widget/richtext.go:57
fyne.io/fyne/v2/widget.NewRichTextWithText({0xbb6eb1, 0x38})
	/home/wmoran/go/pkg/mod/fyne.io/fyne/v2@v2.4.4/widget/richtext.go:66 +0x13d
fyne.io/fyne/v2/widget.(*Label).CreateRenderer(0xc000400000)
	/home/wmoran/go/pkg/mod/fyne.io/fyne/v2@v2.4.4/widget/label.go:69 +0x27
fyne.io/fyne/v2/internal/cache.Renderer({0xcc2310, 0xc000400000})
	/home/wmoran/go/pkg/mod/fyne.io/fyne/v2@v2.4.4/internal/cache/widget.go:33 +0x118
fyne.io/fyne/v2/widget.(*Label).MinSize(0xc000400000)
	/home/wmoran/go/pkg/mod/fyne.io/fyne/v2@v2.4.4/widget/label.go:93 +0x3b
fyne.io/fyne/v2/widget.(*List).CreateRenderer(0xc000000240)
	/home/wmoran/go/pkg/mod/fyne.io/fyne/v2@v2.4.4/widget/list.go:83 +0x6b
fyne.io/fyne/v2/internal/cache.Renderer({0xcc22a8, 0xc000000240})
	/home/wmoran/go/pkg/mod/fyne.io/fyne/v2@v2.4.4/internal/cache/widget.go:33 +0x118
fyne.io/fyne/v2/widget.(*BaseWidget).Refresh(0x0?)
	/home/wmoran/go/pkg/mod/fyne.io/fyne/v2@v2.4.4/widget/widget.go:138 +0x1d
fyne.io/fyne/v2/data/binding.(*listener).DataChanged(0x1?)
	/home/wmoran/go/pkg/mod/fyne.io/fyne/v2@v2.4.4/data/binding/binding.go:56 +0x12
fyne.io/fyne/v2/data/binding.queueItem.func1.1()
	/home/wmoran/go/pkg/mod/fyne.io/fyne/v2@v2.4.4/data/binding/queue.go:19 +0x3c
created by fyne.io/fyne/v2/data/binding.queueItem.func1 in goroutine 1
	/home/wmoran/go/pkg/mod/fyne.io/fyne/v2@v2.4.4/data/binding/queue.go:17 +0x46

How to reproduce

build+run the code

Screenshots

No response

Example code

package main

import (
	"fmt"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/data/binding"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
)

var l0Items []string

func main() {
	for c := 0; c < 10; c++ {
		l0Items = append(l0Items, fmt.Sprintf("L0 Item %d", c))
	}
	const widthString = "Really Long String to try to force the width to be wider"
	listBinding := binding.BindStringList(&l0Items)
	l0 := widget.NewListWithData(
		listBinding,
		func() fyne.CanvasObject {
			return widget.NewLabel(widthString)
		},
		func(i binding.DataItem, o fyne.CanvasObject) {
			o.(*widget.Label).Bind(i.(binding.String))
		},
	)
	layout := container.New(layout.NewVBoxLayout(), l0)
	a := app.New()
	w := a.NewWindow("Experiment")
	w.SetContent(layout)
	w.ShowAndRun()
}

Fyne version

v2.4.4

Go compiler version

go version go1.22.1 linux/amd64

Operating system and version

Ubuntu 22

Additional Information

No response

With the attached PR you now get the following instead of a crash:

2024/04/23 10:21:16 Fyne error:  Attempt to access current Fyne theme when no app is started
2024/04/23 10:21:16   At: /Users/andy/Code/Fyne/fyne/app/settings.go:70

This is now resolved on develop branch and will be in v2.5.0