Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instance derived from Object deleting all its children before destruction #1376

Open
MessyGhost opened this issue Jan 29, 2024 · 2 comments
Open

Comments

@MessyGhost
Copy link

Godot version

4.2

godot-cpp version

4.2

System information

Windows 11

Issue description

Object::~Object() removes all the children and deletes them before the destructor of the derived class. Shouldn't I delete the children myself?

Steps to reproduce

With the code below, destruction of B may lead to a crash. (In my circumstance, the editor crashes when I open the project the second time)

using namespace godot;

class A: public Node {
    GDCLASS(A, Node);

protected:
    static void _bind_methods() {}

public:
    A() {}
};

class B: public Node {
    GDCLASS(B, Node);

protected:
    static void _bind_methods() {}

private:
    A *a;

public:
    B(): a(memnew(A)) { this->add_child(this->a); }

    ~B() {
        // a is removed and deleted before destruction, either of the below may lead to a crash
        // this->remove_child(this->a);
        memdelete(this->a);
    }
};
def _ready():
	B.new()

Minimal reproduction project

N/A

@MessyGhost
Copy link
Author

I just realized that the Object part is destructed before the part of the derived class, which make it nonsense to remove the children in the destructor of the derived class. But it is still confusing that the parent help delete/free all the children.

@AThousandShips
Copy link
Member

This is probably a discussion for the main repo as it's not exclusive to extensions, child nodes are deleted in the pre-delete notification

And I'd say it's not a bug but something that should be documented better, if it's not already

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants