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

Using _ready to remove node added by add_sibling can cause crash #84391

Open
Haydoggo opened this issue Nov 3, 2023 · 0 comments
Open

Using _ready to remove node added by add_sibling can cause crash #84391

Haydoggo opened this issue Nov 3, 2023 · 0 comments

Comments

@Haydoggo
Copy link
Contributor

Haydoggo commented Nov 3, 2023

Godot version

v4.1.3.stable.official [f06b6836a]

System information

Godot v4.1.3.stable - Windows 10.0.19045 - Vulkan (Forward+) - dedicated NVIDIA GeForce GTX 1080 (NVIDIA; 31.0.15.3734) - Intel(R) Core(TM) i5-10600KF CPU @ 4.10GHz (12 Threads)

Issue description

If a Node is added to the scene tree by add_sibling and removed (either by reparenting or removing) from the scene tree during it's own ready callback, a crash can occur. The crash is caused by line 1430 in Node.cpp trying to set the index of the new Node, assuming it knows that it is (or where it is) in the scene tree.

godot/scene/main/node.cpp

Lines 1428 to 1430 in b733901

data.parent->add_child(p_sibling, p_force_readable_name, data.internal_mode);
data.parent->_update_children_cache();
data.parent->_move_child(p_sibling, get_index() + 1);

I opened a pull request (#78834) to fix this in the past, but consensus wasn't reached so I figured the issue should be tracked seperately

Steps to reproduce

Attach the following script to a button and press it a few times:

extends Button

class MyNode extends Node:
	func _ready() -> void:
		get_parent().remove_child(self)

func _pressed() -> void:
	add_sibling(MyNode.new())

Minimal reproduction project

N/A

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants