Finding orphan nodes

Godot Version

4.2.1

Question

I’m accumulating orphans throughout the game, and I would like to free them. I was able to find a lot of places in my code where a orphan node was generated and then I was able to queue_free(). Unfortunately, print_orphan_nodes() doesn’t give more than this information:

0856054946 - Stray Node: (Type: Node)
210906386597 - Stray Node: (Type: Node)
210956718248 - Stray Node: (Type: Node)
211007049899 - Stray Node: (Type: Node)
211057381550 - Stray Node: (Type: Node)
211107713201 - Stray Node: (Type: Node)
211158044852 - Stray Node: (Type: Node)
211208376503 - Stray Node: (Type: Node)
211258708154 - Stray Node: (Type: Node)
211309039805 - Stray Node: (Type: Node)

Do you know any method to find these stray nodes so I can free them?

This post includes an example that uses an autoload singleton. Basically if you have nodes that you are worried about being orphaned, they need to inherit from a script class that can listen to a signal queue_free() them. I’m not aware of any other blanket method like that.

Otherwise I might recommend looking at why your nodes are being orphaned in the first place, and putting in logic to free them instead. Memory leaks suck!

I think I found a way to discover all your orphans. In the main scene of your game (the last one to be exited from the tree), connect the signal tree_exiting and include the method print_orphan_nodes(). Now, run the game and close it whenever you feel you have the number of orphan nodes that you want to be aware of.

1 Like

Ooh that’s nice, could you avoid a code snippet? I’m not sure how I’d connect print_orphan_nodes in the way you’re thinking.

Just use the signals tab in the editor, you should have the tree_exiting signal there: You would have something like this:

func _on_Main_tree_exiting() -> void:
    print_orphan_nodes()

It also helps to give a name to your potential culprits orphan nodes, since might be difficult to see which node is.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.