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

fix occasional crash moving ui root nodes #11371

Merged
merged 1 commit into from
Jan 17, 2024

Conversation

robtfm
Copy link
Contributor

@robtfm robtfm commented Jan 16, 2024

Objective

fix an occasional crash when moving ui root nodes between cameras.

occasionally, updating the TargetCamera of a ui element and then removing the element causes a crash.

i believe that is because when we assign a child in taffy, the old parent doesn't remove that child from it's children, so we have:

user: create root node N1, camera A
-> layout::set_camera_children(A) : 
	- create implicit node A1
	- assign 1 as child -> taffy.children[A1] = [N1], taffy.parents[1] = A1

user: move root node N1 to camera B
-> layout::set_camera_children(B) :
	- create implicit node B1
	- assign 1 as child -> taffy.children[A1] = [N1], taffy.children[B1] = [N1], taffy.parents[1] = B1
-> layout::set_camera_children(A) :
	- remove implicit node A1 (which still has N1 as a child) -> 
		-> taffy sets parent[N1] = None ***
		-> taffy.children[B1] = [N1], taffy.parents[1] = None

user: remove N1
-> layout::remove_entities(N1)
	- since parent[N1] is None, it's not removed from B1 -> taffy.children[B1] = [N1], taffy.parents[1] is removed
-> layout::set_camera_children(B)
	- remove implicit node B1
	- taffy crash accessing taffy.parents[N1]

Solution

we can work around this by making sure to remove the child from the old parent if one exists (this pr).

i think a better fix may be for taffy to check in Taffy::remove and only set the child's parent to None if it is currently equal to the node being removed but i'm not sure if there's an explicit assumption we're violating here (@nicoburns).

@robtfm robtfm added C-Bug An unexpected or incorrect behavior A-UI Graphical user interfaces, styles, layouts, and widgets labels Jan 16, 2024
@nicoburns
Copy link
Contributor

i think a better fix may be for taffy to check in Taffy::remove and only set the child's parent to None if it is currently equal to the node being removed but i'm not sure if there's an explicit assumption we're violating here (@nicoburns).

Taffy's tree-manipulation methods need a review in general, and probably to moved closer to a browser-like API where there is no "set_parent", only "appendChild" (possibly with a set of paralell "unsafe/unchecked" methods where explicit invariants must be upheld). For the time being, your suggested workaround seems sensible.

@nicoburns nicoburns added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Jan 17, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jan 17, 2024
Merged via the queue into bevyengine:main with commit 30940e5 Jan 17, 2024
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Bug An unexpected or incorrect behavior S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants