Skip to content

Commit 417665d

Browse files
authored
fix: Crash on exit on Android (#439)
1 parent 10a2a63 commit 417665d

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixes
66

77
- Fixed SentryLog.set_attribute() not working on Android ([#438](https://github.com/getsentry/sentry-godot/pull/438))
8+
- Fixed occasional crash on exit on Android ([#439](https://github.com/getsentry/sentry-godot/pull/439))
89

910
## 1.1.0
1011

src/sentry/android/android_sdk.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,31 @@ void SentryAndroidBeforeSendLogHandler::_bind_methods() {
7777
// *** AndroidSDK
7878

7979
void AndroidSDK::set_context(const String &p_key, const Dictionary &p_value) {
80+
Object *android_plugin = _get_android_plugin();
8081
ERR_FAIL_NULL(android_plugin);
8182
android_plugin->call(ANDROID_SN(setContext), p_key, sanitize_variant(p_value));
8283
}
8384

8485
void AndroidSDK::remove_context(const String &p_key) {
86+
Object *android_plugin = _get_android_plugin();
8587
ERR_FAIL_NULL(android_plugin);
8688
android_plugin->call(ANDROID_SN(removeContext), p_key);
8789
}
8890

8991
void AndroidSDK::set_tag(const String &p_key, const String &p_value) {
92+
Object *android_plugin = _get_android_plugin();
9093
ERR_FAIL_NULL(android_plugin);
9194
android_plugin->call(ANDROID_SN(setTag), p_key, p_value);
9295
}
9396

9497
void AndroidSDK::remove_tag(const String &p_key) {
98+
Object *android_plugin = _get_android_plugin();
9599
ERR_FAIL_NULL(android_plugin);
96100
android_plugin->call(ANDROID_SN(removeTag), p_key);
97101
}
98102

99103
void AndroidSDK::set_user(const Ref<SentryUser> &p_user) {
104+
Object *android_plugin = _get_android_plugin();
100105
ERR_FAIL_NULL(android_plugin);
101106

102107
if (p_user.is_valid()) {
@@ -111,25 +116,29 @@ void AndroidSDK::set_user(const Ref<SentryUser> &p_user) {
111116
}
112117

113118
void AndroidSDK::remove_user() {
119+
Object *android_plugin = _get_android_plugin();
114120
ERR_FAIL_NULL(android_plugin);
115121
android_plugin->call(ANDROID_SN(removeUser));
116122
}
117123

118124
Ref<SentryBreadcrumb> AndroidSDK::create_breadcrumb() {
125+
Object *android_plugin = _get_android_plugin();
119126
ERR_FAIL_NULL_V(android_plugin, nullptr);
120127
int32_t handle = android_plugin->call(ANDROID_SN(createBreadcrumb));
121128
Ref<AndroidBreadcrumb> crumb = memnew(AndroidBreadcrumb(android_plugin, handle));
122129
return crumb;
123130
}
124131

125132
void AndroidSDK::add_breadcrumb(const Ref<SentryBreadcrumb> &p_breadcrumb) {
133+
Object *android_plugin = _get_android_plugin();
126134
ERR_FAIL_NULL(android_plugin);
127135
Ref<AndroidBreadcrumb> crumb = p_breadcrumb;
128136
ERR_FAIL_COND(crumb.is_null());
129137
android_plugin->call(ANDROID_SN(addBreadcrumb), crumb->get_handle());
130138
}
131139

132140
void AndroidSDK::log(LogLevel p_level, const String &p_body, const Dictionary &p_attributes) {
141+
Object *android_plugin = _get_android_plugin();
133142
ERR_FAIL_NULL(android_plugin);
134143

135144
if (p_body.is_empty()) {
@@ -152,23 +161,27 @@ void AndroidSDK::log(LogLevel p_level, const String &p_body, const Dictionary &p
152161
}
153162

154163
String AndroidSDK::capture_message(const String &p_message, Level p_level) {
164+
Object *android_plugin = _get_android_plugin();
155165
ERR_FAIL_NULL_V(android_plugin, String());
156166
return android_plugin->call(ANDROID_SN(captureMessage), p_message, p_level);
157167
}
158168

159169
String AndroidSDK::get_last_event_id() {
170+
Object *android_plugin = _get_android_plugin();
160171
ERR_FAIL_NULL_V(android_plugin, String());
161172
return android_plugin->call(ANDROID_SN(getLastEventId));
162173
}
163174

164175
Ref<SentryEvent> AndroidSDK::create_event() {
176+
Object *android_plugin = _get_android_plugin();
165177
ERR_FAIL_NULL_V(android_plugin, nullptr);
166178
int32_t event_handle = android_plugin->call(ANDROID_SN(createEvent));
167179
Ref<AndroidEvent> event = memnew(AndroidEvent(android_plugin, event_handle));
168180
return event;
169181
}
170182

171183
String AndroidSDK::capture_event(const Ref<SentryEvent> &p_event) {
184+
Object *android_plugin = _get_android_plugin();
172185
ERR_FAIL_NULL_V(android_plugin, String());
173186
ERR_FAIL_COND_V(p_event.is_null(), String());
174187
Ref<AndroidEvent> android_event = p_event;
@@ -179,6 +192,7 @@ String AndroidSDK::capture_event(const Ref<SentryEvent> &p_event) {
179192
}
180193

181194
void AndroidSDK::capture_feedback(const Ref<SentryFeedback> &p_feedback) {
195+
Object *android_plugin = _get_android_plugin();
182196
ERR_FAIL_NULL(android_plugin);
183197
ERR_FAIL_COND_MSG(p_feedback.is_null(), "Sentry: Can't capture feedback - feedback object is null.");
184198
ERR_FAIL_COND_MSG(p_feedback->get_message().is_empty(), "Sentry: Can't capture feedback - feedback message is empty.");
@@ -190,6 +204,8 @@ void AndroidSDK::capture_feedback(const Ref<SentryFeedback> &p_feedback) {
190204
}
191205

192206
void AndroidSDK::add_attachment(const Ref<SentryAttachment> &p_attachment) {
207+
Object *android_plugin = _get_android_plugin();
208+
ERR_FAIL_NULL(android_plugin);
193209
ERR_FAIL_COND(p_attachment.is_null());
194210

195211
if (p_attachment->get_path().is_empty()) {
@@ -211,6 +227,7 @@ void AndroidSDK::add_attachment(const Ref<SentryAttachment> &p_attachment) {
211227
}
212228

213229
void AndroidSDK::init(const PackedStringArray &p_global_attachments, const Callable &p_configuration_callback) {
230+
Object *android_plugin = _get_android_plugin();
214231
ERR_FAIL_NULL(android_plugin);
215232

216233
if (p_configuration_callback.is_valid()) {
@@ -251,20 +268,23 @@ void AndroidSDK::init(const PackedStringArray &p_global_attachments, const Calla
251268
}
252269

253270
void AndroidSDK::close() {
271+
Object *android_plugin = _get_android_plugin();
254272
if (android_plugin != nullptr) {
255273
android_plugin->call(ANDROID_SN(close));
256274
}
257275
}
258276

259277
bool AndroidSDK::is_enabled() const {
278+
Object *android_plugin = _get_android_plugin();
260279
return android_plugin && android_plugin->call(ANDROID_SN(isEnabled));
261280
}
262281

263282
AndroidSDK::AndroidSDK() {
264283
AndroidStringNames::create_singleton();
265284

266-
android_plugin = Engine::get_singleton()->get_singleton("SentryAndroidGodotPlugin");
285+
Object *android_plugin = Engine::get_singleton()->get_singleton("SentryAndroidGodotPlugin");
267286
ERR_FAIL_NULL_MSG(android_plugin, "Sentry: Unable to locate SentryAndroidGodotPlugin singleton.");
287+
android_plugin_instance_id = android_plugin->get_instance_id();
268288

269289
before_send_handler = memnew(SentryAndroidBeforeSendHandler);
270290
before_send_handler->_initialize(android_plugin);
@@ -274,10 +294,6 @@ AndroidSDK::AndroidSDK() {
274294
}
275295

276296
AndroidSDK::~AndroidSDK() {
277-
if (is_enabled()) {
278-
close();
279-
}
280-
281297
AndroidStringNames::destroy_singleton();
282298
if (before_send_handler) {
283299
memdelete(before_send_handler);

src/sentry/android/android_sdk.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ class SentryAndroidBeforeSendLogHandler : public Object {
4040
// Internal SDK utilizing Sentry Android (sentry-java repo).
4141
class AndroidSDK : public InternalSDK {
4242
private:
43-
Object *android_plugin = nullptr;
43+
uint64_t android_plugin_instance_id = 0;
4444
SentryAndroidBeforeSendHandler *before_send_handler = nullptr;
4545
SentryAndroidBeforeSendLogHandler *before_send_log_handler = nullptr;
4646

47+
_FORCE_INLINE_ Object *_get_android_plugin() const { return ObjectDB::get_instance(android_plugin_instance_id); }
48+
4749
public:
4850
virtual void set_context(const String &p_key, const Dictionary &p_value) override;
4951
virtual void remove_context(const String &p_key) override;
@@ -73,7 +75,7 @@ class AndroidSDK : public InternalSDK {
7375
virtual void close() override;
7476
virtual bool is_enabled() const override;
7577

76-
bool has_android_plugin() const { return android_plugin != nullptr; }
78+
bool has_android_plugin() const { return _get_android_plugin() != nullptr; }
7779

7880
AndroidSDK();
7981
virtual ~AndroidSDK() override;

src/sentry/logging/sentry_godot_logger.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ void SentryGodotLogger::_apply_normal_limits() {
297297
void SentryGodotLogger::_log_error(const String &p_function, const String &p_file, int32_t p_line,
298298
const String &p_code, const String &p_rationale, bool p_editor_notify, int32_t p_error_type,
299299
const TypedArray<Ref<ScriptBacktrace>> &p_script_backtraces) {
300+
if (!SentrySDK::get_singleton()) {
301+
return;
302+
}
303+
300304
static thread_local uint32_t num_entries = 0;
301305
constexpr uint32_t MAX_ENTRIES = 5;
302306
RecursionGuard feedback_loop_guard{ &num_entries, MAX_ENTRIES };
@@ -445,6 +449,10 @@ void SentryGodotLogger::_log_error(const String &p_function, const String &p_fil
445449
void SentryGodotLogger::_log_message(const String &p_message, bool p_error) {
446450
sentry::logging::MessageScope message_scope;
447451

452+
if (!SentrySDK::get_singleton()) {
453+
return;
454+
}
455+
448456
bool as_log = SentryOptions::get_singleton()->get_experimental()->get_enable_logs();
449457
bool as_breadcrumb = SentryOptions::get_singleton()->is_logger_messages_as_breadcrumbs_enabled();
450458

0 commit comments

Comments
 (0)