Skip to content

Commit 96bea8f

Browse files
authored
fix: Crash in SentryLogger if removed early (#323)
1 parent 485a7e4 commit 96bea8f

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
- Improve initialization flow ([#322](https://github.com/getsentry/sentry-godot/pull/322))
1212

13+
### Fixes
14+
15+
- Potential crash in SentryLogger if removed early ([#323](https://github.com/getsentry/sentry-godot/pull/323))
16+
1317
### Dependencies
1418

1519
- Bump Cocoa SDK from v8.54.0 to v8.55.0 ([#318](https://github.com/getsentry/sentry-godot/pull/318))

src/sentry/sentry_logger.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ std::size_t SentryLogger::ErrorKeyHash::operator()(const ErrorKey &p_key) const
191191
}
192192

193193
void SentryLogger::_connect_process_frame() {
194-
SceneTree *scene_tree = Object::cast_to<SceneTree>(Engine::get_singleton()->get_main_loop());
195-
if (scene_tree) {
196-
Callable callable = callable_mp(this, &SentryLogger::_process_frame);
197-
if (!scene_tree->is_connected("process_frame", callable)) {
198-
scene_tree->connect("process_frame", callable);
199-
}
200-
} else {
201-
ERR_PRINT("Sentry: Failed to connect `process_frame` signal – main loop is null");
194+
MainLoop *main_loop = Engine::get_singleton()->get_main_loop();
195+
ERR_FAIL_NULL_MSG(main_loop, "SentryLogger: Failed to connect to \"process_frame\" signal - main loop is null.");
196+
SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
197+
ERR_FAIL_NULL_MSG(scene_tree, "SentryLogger: Failed to connect to \"process_frame\" signal - expected SceneTree instance as main loop.");
198+
199+
Callable callable = callable_mp(this, &SentryLogger::_process_frame);
200+
if (!scene_tree->is_connected("process_frame", callable)) {
201+
scene_tree->connect("process_frame", callable);
202202
}
203203
}
204204

@@ -364,10 +364,20 @@ void SentryLogger::_log_message(const String &p_message, bool p_error) {
364364
"debug");
365365
}
366366

367+
void SentryLogger::_bind_methods() {
368+
ClassDB::bind_method(D_METHOD("_connect_process_frame"), &SentryLogger::_connect_process_frame);
369+
}
370+
367371
void SentryLogger::_notification(int p_what) {
368372
switch (p_what) {
369373
case NOTIFICATION_POSTINITIALIZE: {
370-
callable_mp(this, &SentryLogger::_connect_process_frame).call_deferred();
374+
SceneTree *scene_tree = Object::cast_to<SceneTree>(Engine::get_singleton()->get_main_loop());
375+
if (scene_tree) {
376+
_connect_process_frame();
377+
} else {
378+
// Defer signal connection since SceneTree is not available during early initialization.
379+
call_deferred("_connect_process_frame");
380+
}
371381
} break;
372382
case NOTIFICATION_PREDELETE: {
373383
_disconnect_process_frame();
@@ -394,12 +404,6 @@ SentryLogger::~SentryLogger() {
394404
if (!Engine::get_singleton()) {
395405
return;
396406
}
397-
398-
SceneTree *scene_tree = Object::cast_to<SceneTree>(Engine::get_singleton()->get_main_loop());
399-
Callable callable = callable_mp(this, &SentryLogger::_process_frame);
400-
if (scene_tree && scene_tree->is_connected("process_frame", callable)) {
401-
scene_tree->disconnect("process_frame", callable);
402-
}
403407
}
404408

405409
} // namespace sentry

src/sentry/sentry_logger.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class SentryLogger : public Logger {
6161
void _process_frame();
6262

6363
protected:
64-
static void _bind_methods() {}
64+
static void _bind_methods();
6565

6666
void _notification(int p_what);
6767

0 commit comments

Comments
 (0)