Skip to content

Commit 9db20f2

Browse files
committed
Fix global static body memory leak
1 parent 1b1761c commit 9db20f2

File tree

7 files changed

+43
-32
lines changed

7 files changed

+43
-32
lines changed

editor/viewport/editor_camera_settings_4d.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ void EditorCameraSettings4D::setup(Node *p_ancestor_of_cameras, Ref<ConfigFile>
8282
}
8383

8484
void EditorCameraSettings4D::write_to_config_file() const {
85-
_4d_editor_config_file->erase_section("camera");
85+
if (_4d_editor_config_file->has_section("camera")) {
86+
_4d_editor_config_file->erase_section("camera");
87+
}
8688
if (!Math::is_equal_approx(_clip_near, 0.05)) {
8789
_4d_editor_config_file->set_value("camera", "clip_near", _clip_near);
8890
}

editor/viewport/editor_main_screen_4d.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,7 @@ void EditorMainScreen4D::_on_view_menu_id_pressed(const int p_id) {
165165
_4d_editor_config_file->save(_4d_editor_config_file_path);
166166
} break;
167167
case VIEW_ITEM_CAMERA_SETTINGS: {
168-
#if GODOT_VERSION_MAJOR > 4 || (GODOT_VERSION_MAJOR == 4 && GODOT_VERSION_MINOR >= 4)
169168
_camera_settings_dialog->popup_centered(Size2(400, 300) * EDSCALE);
170-
#endif
171169
} break;
172170
}
173171
}
@@ -428,15 +426,15 @@ void EditorMainScreen4D::setup(EditorUndoRedoManager *p_undo_redo_manager) {
428426

429427
_camera_settings = memnew(EditorCameraSettings4D);
430428
_camera_settings->setup(this, _4d_editor_config_file, _4d_editor_config_file_path);
431-
#if GODOT_VERSION_MAJOR > 4 || (GODOT_VERSION_MAJOR == 4 && GODOT_VERSION_MINOR >= 4)
432-
// Set up the camera settings dialog as long as this is Godot >= 4.4.
433429
_camera_settings_dialog = memnew(ConfirmationDialog);
434430
_camera_settings_dialog->set_name(StringName("CameraSettingsDialog"));
435431
_camera_settings_dialog->set_title(TTR("Editor Camera Settings"));
436432
_camera_settings_inspector = memnew(EditorInspector);
437433
_camera_settings_inspector->set_name(StringName("CameraSettingsInspector"));
438434
_camera_settings_inspector->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
439435
_camera_settings_inspector->set_custom_minimum_size(Size2(400, 200) * EDSCALE);
436+
#if GODOT_MODULE || (GODOT_VERSION_MAJOR > 4 || (GODOT_VERSION_MAJOR == 4 && GODOT_VERSION_MINOR >= 4))
437+
// Set up the camera settings dialog as long as this is Godot >= 4.4.
440438
_camera_settings_inspector->edit(_camera_settings);
441439
_camera_settings_dialog->add_child(_camera_settings_inspector);
442440
add_child(_camera_settings_dialog);
@@ -454,8 +452,10 @@ void EditorMainScreen4D::setup(EditorUndoRedoManager *p_undo_redo_manager) {
454452
}
455453

456454
EditorMainScreen4D::~EditorMainScreen4D() {
457-
#if GODOT_VERSION_MAJOR > 4 || (GODOT_VERSION_MAJOR == 4 && GODOT_VERSION_MINOR >= 4)
455+
#if GODOT_MODULE || (GODOT_VERSION_MAJOR > 4 || (GODOT_VERSION_MAJOR == 4 && GODOT_VERSION_MINOR >= 4))
458456
_camera_settings_inspector->edit(nullptr);
459-
memdelete(_camera_settings);
460457
#endif
458+
if (_camera_settings != nullptr) {
459+
memdelete(_camera_settings);
460+
}
461461
}

godot_4d_defines.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ using namespace godot;
4040
#define GODOT_VERSION_PATCH VERSION_PATCH
4141
#endif
4242

43-
#if GODOT_VERSION_MAJOR == 4 && GODOT_VERSION_MINOR < 5
44-
// Prior to Godot 4.5, the internal API of free_rid in RenderingServer and other servers did not match the exposed API.
43+
#if GODOT_VERSION_MAJOR == 4 && GODOT_VERSION_MINOR < 6
44+
// Prior to Godot 4.6, the internal API of free_rid in RenderingServer and other servers did not match the exposed API.
4545
// See https://github.com/godotengine/godot/pull/107139
4646
#define free_rid free
4747
#endif

physics/collision_shape_4d.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,22 @@
11
#include "collision_shape_4d.h"
22

33
#include "bodies/collision_object_4d.h"
4-
#include "bodies/static_body_4d.h"
54
#include "server/physics_server_4d.h"
65

7-
CollisionObject4D *CollisionShape4D::_global_static_body_for_bodyless_shapes = nullptr;
8-
9-
CollisionObject4D *CollisionShape4D::_get_or_create_global_static_body() {
10-
if (_global_static_body_for_bodyless_shapes != nullptr) {
11-
return _global_static_body_for_bodyless_shapes;
12-
}
13-
StaticBody4D *global_static_body = memnew(StaticBody4D);
14-
global_static_body->set_name("_GlobalStaticBody4D");
15-
PhysicsServer4D::get_singleton()->register_physics_body(global_static_body);
16-
_global_static_body_for_bodyless_shapes = global_static_body;
17-
return _global_static_body_for_bodyless_shapes;
18-
}
19-
206
CollisionObject4D *CollisionShape4D::_get_ancestor_collision_object() const {
217
Node *parent = get_parent();
228
while (parent) {
239
Node4D *parent_4d = Object::cast_to<Node4D>(parent);
2410
if (unlikely(!parent_4d)) {
25-
return _get_or_create_global_static_body();
11+
return PhysicsServer4D::get_or_create_global_static_body();
2612
}
2713
CollisionObject4D *co = Object::cast_to<CollisionObject4D>(parent);
2814
if (likely(co)) {
2915
return co;
3016
}
3117
parent = parent->get_parent();
3218
}
33-
return _get_or_create_global_static_body();
19+
return PhysicsServer4D::get_or_create_global_static_body();
3420
}
3521

3622
void CollisionShape4D::_notification(int p_what) {

physics/collision_shape_4d.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#endif
1111

1212
class CollisionObject4D;
13+
class PhysicsServer4D;
1314

1415
class CollisionShape4D : public Node4D {
1516
GDCLASS(CollisionShape4D, Node4D);
@@ -20,8 +21,6 @@ class CollisionShape4D : public Node4D {
2021
uint32_t _collision_layer = 1;
2122
uint32_t _collision_mask = 1;
2223

23-
static CollisionObject4D *_global_static_body_for_bodyless_shapes;
24-
static CollisionObject4D *_get_or_create_global_static_body();
2524
CollisionObject4D *_get_ancestor_collision_object() const;
2625

2726
protected:

physics/server/physics_server_4d.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "physics_server_4d.h"
22

3+
#include "../bodies/static_body_4d.h"
4+
35
#if GDEXTENSION
46
#include <godot_cpp/classes/scene_tree.hpp>
57
#include <godot_cpp/classes/window.hpp>
@@ -145,7 +147,27 @@ TypedArray<PhysicsBody4D> PhysicsServer4D::get_physics_body_nodes() const {
145147
return _physics_body_nodes;
146148
}
147149

148-
PhysicsServer4D *PhysicsServer4D::singleton = nullptr;
150+
CollisionObject4D *PhysicsServer4D::get_or_create_global_static_body() {
151+
if (_global_static_body_for_bodyless_shapes != nullptr) {
152+
return _global_static_body_for_bodyless_shapes;
153+
}
154+
StaticBody4D *global_static_body = memnew(StaticBody4D);
155+
global_static_body->set_name("_GlobalStaticBody4D");
156+
PhysicsServer4D::get_singleton()->register_physics_body(global_static_body);
157+
_global_static_body_for_bodyless_shapes = global_static_body;
158+
return _global_static_body_for_bodyless_shapes;
159+
}
160+
161+
CollisionObject4D *PhysicsServer4D::_global_static_body_for_bodyless_shapes = nullptr;
162+
PhysicsServer4D *PhysicsServer4D::_singleton = nullptr;
163+
164+
PhysicsServer4D::~PhysicsServer4D() {
165+
_singleton = nullptr;
166+
if (_global_static_body_for_bodyless_shapes != nullptr) {
167+
memdelete(_global_static_body_for_bodyless_shapes);
168+
_global_static_body_for_bodyless_shapes = nullptr;
169+
}
170+
}
149171

150172
void PhysicsServer4D::_bind_methods() {
151173
ClassDB::bind_method(D_METHOD("register_physics_engine", "name", "engine"), &PhysicsServer4D::register_physics_engine);

physics/server/physics_server_4d.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ class PhysicsServer4D : public Object {
3535
void _physics_process();
3636

3737
protected:
38-
static PhysicsServer4D *singleton;
38+
static CollisionObject4D *_global_static_body_for_bodyless_shapes;
39+
static PhysicsServer4D *_singleton;
3940
static void _bind_methods();
4041

4142
public:
@@ -62,7 +63,8 @@ class PhysicsServer4D : public Object {
6263
TypedArray<Area4D> get_area_nodes() const;
6364
TypedArray<PhysicsBody4D> get_physics_body_nodes() const;
6465

65-
static PhysicsServer4D *get_singleton() { return singleton; }
66-
PhysicsServer4D() { singleton = this; }
67-
~PhysicsServer4D() { singleton = nullptr; }
66+
static CollisionObject4D *get_or_create_global_static_body();
67+
static PhysicsServer4D *get_singleton() { return _singleton; }
68+
PhysicsServer4D() { _singleton = this; }
69+
~PhysicsServer4D();
6870
};

0 commit comments

Comments
 (0)