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

Container.Remove() race causes crash #2826

Closed
arma7x opened this issue Mar 7, 2022 · 8 comments
Closed

Container.Remove() race causes crash #2826

arma7x opened this issue Mar 7, 2022 · 8 comments
Labels
blocker Items that would block a forthcoming release bug Something isn't working

Comments

@arma7x
Copy link

arma7x commented Mar 7, 2022

Describe the bug:

  • Execute Container.Remove(object) by loop over Container.Objects to remove all Object does not work as expected

To Reproduce:

Steps to reproduce the behaviour:

  1. Click Populate Cards to populate dummy cards
  2. Click Remove All Cards(bug) to reproduce the bug
  3. Click Remove All Cards(temporary solution)

Example code:

https://go.dev/play/p/7wYM2bYBSaP

Device (please complete the following information):

  • OS: Lubuntu
  • Version: 18.04.6 LTS
  • Go version: go1.17.7 linux/amd64
  • Fyne version: v2 v2.1.2
@arma7x arma7x added the unverified A bug that has been reported but not verified label Mar 7, 2022
@Jacalz Jacalz added bug Something isn't working and removed unverified A bug that has been reported but not verified labels Mar 7, 2022
@arma7x
Copy link
Author

arma7x commented Mar 7, 2022

Sometime panic: runtime error: invalid memory address or nil pointer dereference if the Container.Objects was too big
https://go.dev/play/p/-68fye203eh

@Jacalz
Copy link
Member

Jacalz commented Mar 7, 2022

I’ve tried looking at this but I don’t understand why it is behaving the way it is. Something is strange with the container remove code. This is probably related to #2775.

We should obviously track down the cause of this issue, but I would just like to clarify something for future readers. For removing everything at once, the method used above is terribly inefficient. I’d suggest the use of one of the methods mentioned in https://yourbasic.org/golang/clear-slice/ instead. Using that on the container .Objects slice should be the best solution.

@arma7x
Copy link
Author

arma7x commented Mar 7, 2022

maybe iterate range of current Container.Objects is bad approach

@arma7x arma7x closed this as completed Mar 7, 2022
@Jacalz
Copy link
Member

Jacalz commented Mar 7, 2022

We should probably keep this open until the underlying issues have been fixed.

@Jacalz Jacalz reopened this Mar 7, 2022
@arma7x
Copy link
Author

arma7x commented Mar 7, 2022

I am not a native english speaker, but this is my explanation https://go.dev/play/p/Kv1G_WSsgWE

@andydotxyz
Copy link
Member

It is much easier to set Container.Objects = nil to remove them all :)

@scmu1
Copy link

scmu1 commented Mar 10, 2022

I'm not a english speaker, please forgive my english. Yesterday i found this bug too. Now i find bug reason.

  1. Main thread in loop refresh all canvas objects and call Layout.Layout() refresh layout position and size
  2. container.Remove() method manipulate c.Objects directly, c.Objects[len(c.Objects)-1] = nil set latest object nill, it cause main thread call Layout.Layout() get c.Objects latest nil object then panic
		copy(c.Objects[i:], c.Objects[i+1:])

		c.Objects[len(c.Objects)-1] = nil     //  latest object set to nil
		c.Objects = c.Objects[:len(c.Objects)-1]

I rewrite container.Remove() delete object logic and no nil pointer error

		copy(c.Objects[i:], c.Objects[i+1:])
		// set temp array remove object
		objs := make([]CanvasObject, len(c.Objects))
		for j, o := range c.Objects {
			objs[j] = o
		}

		objs[len(objs)-1] = nil
		objs = objs[:len(objs)-1]
		c.Objects = objs

@Jacalz Jacalz added this to the Fixes (v2.1.x) milestone Mar 11, 2022
@Jacalz Jacalz added the blocker Items that would block a forthcoming release label Mar 11, 2022
@Jacalz Jacalz changed the title Container.Remove() bug Container.Remove() race causes crash Mar 11, 2022
andydotxyz added a commit to andydotxyz/fyne that referenced this issue Mar 13, 2022
andydotxyz added a commit that referenced this issue Mar 14, 2022
@andydotxyz
Copy link
Member

Ready on release/v2.1.x for 2.1.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocker Items that would block a forthcoming release bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants