-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
widgets still displayed after being removed from fyne.Container.Objects #2103
Comments
I think the crash you got is due to a race condition, as you are removing objects from the container while in the background those objects are used to render the screen.
This is because currently Nevertheless, for the use case you are trying to achieve, I think you should try |
No, calling main.gopackage main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/widget"
)
func main() {
w := app.New().NewWindow("test")
input := binding.NewString()
outBox := container.NewVBox()
w.SetContent(container.NewBorder(
container.NewVBox(
widget.NewEntryWithData(input),
widget.NewButton("Search", func() {
s, err := input.Get()
if err != nil {
panic(err)
}
for _, v := range outBox.Objects {
outBox.Remove(v)
}
//dict is [][2]string, as defined in data.go
for _, v := range dict {
if v[0] == s {
label := widget.NewLabel(v[1])
label.Wrapping = fyne.TextWrapWord
outBox.Add(label)
outBox.Add(widget.NewSeparator())
}
}
outBox.Refresh()
})),
nil,
nil,
nil,
container.NewVScroll(outBox),
))
w.ShowAndRun()
}
Thanks for the hint, I'll try it. |
The outBox.Objects = []fyne.CanvasObject{label}
outBox.Refresh() |
This is surely a better approach and doing it solves both the problems I faced. However using |
You are quite right that it should not crash. |
There is no for _, v := range outBox.Objects {
outBox.Remove(v)
} is just a shorthand to for i := 0; i < len(outBox.Objects); i++ {
outBox.Remove(outBox.Objects[i])
} so the index is incremented while the slice is shifted to the left, and obviously some elements are skipped. If one wants to remove all the objects using for len(outBox.Objects) > 0 {
outBox.Remove(outBox.Objects[0])
} This seems to be unrelated to the crash, which is caused by an already known bug, so I'm closing the issue. |
Thanks for the clarification there @4ricci, very helpful |
Describe the bug:
In the example code below, every time the button is pressed, the contents of outBox.Objects will be eventually removed and replaced with new labels and separators. The code does not work as expected and sometimes also crashes.
With the same code, I also reproduced #1897 and #1946.
To Reproduce:
Steps to reproduce the behaviour:
A label with the corresponding text and a separator are displayed. This is OK.
A separator, a label with the text corresponding to "b" and another separator are displayed. This is not OK, the first separator should not be there.
A label with "B..." text, a label with "A..." text and a separator are displayed. It seems that one of the old widgets is still displayed after being passed to .Remove().
The program panics and something like the following is printed on the terminal:
panic
It is interesting to note that the longer is the text, the easier is to crash the program. What's in the file
data.go
(which is not exceptionally long) requires only a few clicks, at least on my machine. Shorter text requires to click very fast to get a crash.The crash could be related to #1957, but I don't know why the widgets are not removed from the screen. There are issues reporting memory leaks, but in theory a memory leak only affects memory usage, not the behaviour of the program.
Example code:
main.go
data.go
Device:
I was able to reproduce the behaviour on the following:
The text was updated successfully, but these errors were encountered: