Best practices for cleanup on game exit

I read this page: Handling quit requests — Godot Engine (stable) documentation in English

However, it does not state whether I need to clean up anything and why. I expect that very often developers just do get_tree().quit() and that’s it. But recently I noticed ‘leaked resources’ warnings in the console and now I wonder:

  • Do I need to call queue_free() on each node before quitting?
  • Do I need to implement _notification on all my nodes to cleanup on NOTIFICATION_WM_CLOSE_REQUEST ?
  • Do I actually need to aim for 0 leaked-warnings in console? If yes, then why?

My expectation would be that quit() transitively calls queue_free() on all nodes (or on scene root which, as I expect, the same)

1 Like

Nope, you do not need to use anything before quit, otherwise NOTIFICATION_WM_CLOSE_REQUEST is when you quitting the game, you can run codes in _notification like you made a code editor where your codes is unsaved, so when user quit the editor, it will not quit and a message shows that do you sure to quit? All you can do in _notification. Like I have made this code editor using Godot

1 Like

Then what’s the sense in logging all these leak-warnings? Isn’t this just misleading developers?

Without details on your actual project it’s hard to tell what isn’t cleaned up, do you have circular references for example? They will cause leaks, but I don’t get leaks on normal use so this sounds like a project specific issue

You don’t need to clean up anything explicitly assuming you close down safely and if you don’t have incorrect resource use

I have object pools - and I assume that I maybe have to clear their internal arrays of instantiated Nodes to have no leaks? And again, one of the main questions is:

  • Do I need to worry about leak-warnings? Do they mean I’m open to sudden game crashes and, therefore, I’d better fix all these leak-warnings? Or maybe they are not “critical”, meaning, the resources will be cleaned up for me on actual exit anyway?

UP

Some entities do have circular references (e.g. a creature knows its spawner and a spawner knows all its creatures). Do I need to null-ify these references on exit?

Yes, or better to use weakref to handle these as you really shouldn’t use circular references, they’re not supported

Thank you for this response. I will mark it as solution, but can you please elaborate on this?

Do I need to worry about leak-warnings? Do they mean I’m open to sudden game crashes and, therefore, I’d better fix all these leak-warnings?

Are these warnings should really be treated and not ignored?

Depends, if it happens when the engine closes down normally they are relevant to care about, if they’re data loaded in scenes and on scene change, that would mean leaks when loading different scenes, causing memory to run out gradually, so it’s not always critical, but you shouldn’t ignore them if you can fix them and they’re due to your code

1 Like