Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segfault when adding items directly in form struct #3153

Closed
2 tasks done
paupin2 opened this issue Jul 21, 2022 · 4 comments · Fixed by #3185
Closed
2 tasks done

Segfault when adding items directly in form struct #3153

paupin2 opened this issue Jul 21, 2022 · 4 comments · Fixed by #3185
Labels
unverified A bug that has been reported but not verified

Comments

@paupin2
Copy link

paupin2 commented Jul 21, 2022

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

I get a SEGFAULT when calling .Refresh() on a form where all items were all added in the struct creation.

I followed this example, but added everything as Items when creating the struct -- so no form.Append().

This issue may be the same as #1967, but I'm not sure.

The problem seems to be when ensureRenderItems assumes itemGrid is not null. This is not the case when adding items directly to the form struct.

If adding items directly is not recommended, maybe the tutorial could be updated.
But a guard on this code would probably be safer.

How to reproduce

Call start(false) on the example code below and it works fyne.
Call start(true) on and it segfaults.

Screenshots

No response

Example code

import (
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/widget"
)

func start(segfault bool) {
	a := app.New()
	w := a.NewWindow("Manny")

	name := widget.NewEntry()
	name.SetPlaceHolder("John Smith")
	item := &widget.FormItem{Text: "Name", Widget: name}

	form := &widget.Form{}
	if segfault {
		form.Items = []*widget.FormItem{item}
	} else {
		form.AppendItem(item)
	}
	form.Refresh()

	w.SetContent(form)
	w.ShowAndRun()
}

Fyne version

v2.2.3

Go compiler version

go1.18.4

Operating system

macOS

Operating system version

11.6.5

Additional Information

No response

@paupin2 paupin2 added the unverified A bug that has been reported but not verified label Jul 21, 2022
@andydotxyz
Copy link
Member

Please provide the crash trace

@paupin2
Copy link
Author

paupin2 commented Jul 23, 2022

With the example code:

package main

import (
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/widget"
)

func start(segfault bool) {
	a := app.New()
	w := a.NewWindow("Manny")

	name := widget.NewEntry()
	name.SetPlaceHolder("John Smith")
	item := &widget.FormItem{Text: "Name", Widget: name}

	form := &widget.Form{}
	if segfault {
		form.Items = []*widget.FormItem{item}
	} else {
		form.AppendItem(item)
	}
	form.Refresh()

	w.SetContent(form)
	w.ShowAndRun()
}

func main() {
	start(true)
}

I get:

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

goroutine 1 [running, locked to thread]:
fyne.io/fyne/v2/widget.(*Form).ensureRenderItems(0x0?)
        ~/.asdf/installs/golang/1.18.4/packages/pkg/mod/fyne.io/fyne/v2@v2.2.3/widget/form.go:221 +0x1f
fyne.io/fyne/v2/widget.(*Form).Refresh(0xc00045e0d0)
        ~/.asdf/installs/golang/1.18.4/packages/pkg/mod/fyne.io/fyne/v2@v2.2.3/widget/form.go:93 +0x34
main.start(0x1)
        ~/src/qwfapp/manny/main.go:22 +0x179
main.main()
        ~/src/qwfapp/manny/main.go:29 +0x1e
exit status 2

@Jacalz
Copy link
Member

Jacalz commented Jul 24, 2022

The crash happens because itemGrid hasn’t been created when you refresh (due to the CreateRenderer() method not having been called yet). Technically, you don’t need to call Refresh() there because it hasn’t been rendered yet but it obviously shouldn’t crash if you do.

done := len(f.itemGrid.Objects) / 2

@andydotxyz
Copy link
Member

This was fixed ready for next release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
unverified A bug that has been reported but not verified
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants