Updating to disposing all Godot objects

When checking the update to NetAnalyzers 7 (from 6) it now starts to complain on basically all of our classes that have any NodePath or Control derived fields in it about those not being disposed. It wouldn’t be too difficult to edit (with help from an IDE) to have all of our classes follow the rule of disposing all resources they have. This would have the obvious benefit that any non-Godot classes that are written incorrectly would get caught by the checks.

I can think of two potential downsides:

  • There might be a performance impact compared to letting Godot take care of disposing Node trees. But then again it might actually be more performant to take the C# approach where disposing and destruction of Nodes is triggered that way.
  • Slightly more effort in writing code, but doing things properly instead of taking the easy way out is (almost) always good in my eyes

So does anyone have thoughts on this which direction we should take (disposing stuff properly or suppressing the warning)? Or better yet can someone profile to see if there is any memory or performance benefit in either option? I suspect disposing things early might have a slight memory use reduction.

Thinking about this more, I think we should just not dispose the objects that Godot will handle disposing (by being scene tree attached). I hope that that is either more performant or at least doesn’t incur a penalty. I think it might be the case as disposing stuff will need to destruct nodes individually and they probably need to detach from their parents, in the case of Godot handling destroying entire node trees they may have optimizations in place.

I could make a performance test or something, but I think the better idea is to keep the current approach and suppress the warning.

1 Like

There’s actually one type of object, NodePath where I’m unsure if we should dispose those. Disposing them would probably help with clearing memory sooner as they aren’t part of the node tree or standard properties so they are probably just left to the garbage collector to handle.

I did the changes now:

Mostly just ignored all the Godot Node types and added disposing for all NodePaths. I did find a couple of objects that had stuff they should dispose though.