godotengine / godotengine/godot-docs

`NOTIFICATION_PREDELETE` is recommended to be used like a destructor, but for `Node`s, it happens before `_exit_tree()`.

Open
#11,107 0 comments 1 reaction 0 assignees View on GitHub
area:class reference bug
Dominant language
reStructuredText
Stars
5.7k
Forks
3.8k
Avg merge
1d 20h
Merged PRs (30d)
25

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:**

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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.