From 571e05d3d39d0bbc90e151e822c4002cb8c869e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Wed, 5 Jan 2022 14:00:04 +0100 Subject: [PATCH] Clear only owners that are no longer in the same tree (cherry picked from commit 2db9397fb949086cd403cd6180240334a3272c30) --- scene/main/node.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/scene/main/node.cpp b/scene/main/node.cpp index c51754ba792..c6b1f5972d0 100644 --- a/scene/main/node.cpp +++ b/scene/main/node.cpp @@ -234,15 +234,32 @@ void Node::_propagate_enter_tree() { } void Node::_propagate_after_exit_tree() { + // Clear owner if it was not part of the pruned branch if (data.owner) { - data.owner->data.owned.erase(data.OW); - data.owner = nullptr; + bool found = false; + Node *parent = data.parent; + + while (parent) { + if (parent == data.owner) { + found = true; + break; + } + + parent = parent->data.parent; + } + + if (!found) { + data.owner->data.owned.erase(data.OW); + data.owner = nullptr; + } } + data.blocked++; for (int i = 0; i < data.children.size(); i++) { data.children[i]->_propagate_after_exit_tree(); } data.blocked--; + emit_signal(SceneStringNames::get_singleton()->tree_exited); }