Skip to content

NOTIFICATION_PREDELETE is recommended to be used like a destructor, but for Nodes, it happens before _exit_tree(). #11107

@MajorMcDoom

Description

@MajorMcDoom

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 Objects.

Unfortunately, it turns out that for Nodes, 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:

https://docs.godotengine.org/en/4.4/classes/class_object.html#class-object-constant-notification-predelete

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:class referenceIssues and PRs about the class reference, which should be addressed on the Godot engine repositorybug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions