godotengine / godotengine/godot-cpp

Instance derived from Object deleting all its children before destruction

Open
#1,376 2 comments 1 reaction 0 assignees View on GitHub
discussion documentation
Dominant language
C++
Stars
2.7k
Forks
809
Avg merge
1d 3h
Merged PRs (30d)
8

Description

### 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)
```cpp
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);
}
};

```

```GDScript
def _ready():
B.new()
```

### Minimal reproduction project

N/A

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.