Skip to content

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

Open
@MessyGhost

Description

@MessyGhost

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions