Trying to figure out why I have so many orphaned
objects in my game. As it turns out, whenever you
use duplicate
,
instantiate
, or remove a node from the
scene tree, Godot maintains a reference to that node
even when the script which is holding a reference to
the node is deleted.
You can use print_orphan_nodes()
to
find out what is being leaked (see: https://www.reddit.com/r/godot/comments/f9gsry/comment/firj4w2/).
I also found that there is a orphan count monitor in the Godot debugger.
In no signal, this caused a rather big memory leak as none of the old scenes were ever freed:
remove_child(tree.current_scene) # Doesn't delete the old scene! tree.root.
I needed to do this instead:
free() tree.current_scene.
I also have a frequent pattern where I hold onto a reference or a duplicate of a node instance to use as an easy way to instantiate another instance of that scene tree, but this leaks unless I explicitly free these nodes later.
This is because Node
is not
RefCounted
, so Godot expects you to
manage them explicitly.