-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Description
Your Godot version:
Godot 4.4.1.stable
Issue description:
NOTIFICATION_PREDELETE
is recommended to be used like a destructor. A destructor is the last code that can possibly be executed by a node. Following this recommendation, users would use this method to do things like freeing up allocated Object
s.
Unfortunately, it turns out that for Node
s, this notification fires before _exit_tree()
. This can lead to serious bugs if the user for example the user accesses members in _enter_tree
and _exit_tree
, and makes the "destructor" responsible for freeing these members. In order to avoid such a bug, NOTIFICATION_DELETE
simply cannot be used as a de facto destructor.
As such, I think this documentation is extremely misleading. That said, I cannot fully rule out whether this is an actual order-of-execution issue in the engine, and it actually is supposed to work as the documentation described. But that sounds super unlikely, given that this pertains to core object lifetime notifications.
More info:
You can manually force PREDELETE
to fire last in most cases by always removing a node from the tree before freeing or queue freeing, but that is error-prone, since it is not enforced by the engine, and definitely not by every plugin or extension. Also, it is possible to free nodes from their _ready
function, but it is not possible to remove them from the tree, making it impossible to enforce this strategy in all cases.
URL to the documentation page: