Skip to content

Commit 2eb5f98

Browse files
committed
- android: handle exits correctly, since as stated in SDl2 docs, std::exit() doesn't work, so trowing an exception, catching it in the main a,dn exiting via return there
- core/errors.hpp: add virtual override for std::exception what(), so that it gets a correct message, if it's used wrongly
1 parent 3dfdd87 commit 2eb5f98

File tree

6 files changed

+51
-1
lines changed

6 files changed

+51
-1
lines changed

src/executables/game/application.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,8 @@ void Application::initialize() {
361361
spdlog::debug("Aborted loading after {}", duration);
362362

363363
// just exit immediately, without cleaning up, since than we would have to cancel the loading thread somehow, which is way rto complicated, let the OS clean up our mess we create her xD
364-
std::exit(0);
364+
365+
utils::exit(0);
365366
}
366367

367368

src/executables/game/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ namespace {
129129

130130

131131
return EXIT_FAILURE;
132+
} catch (const utils::ExitException& exit_exception) {
133+
spdlog::debug("Requested exit with status code {}", exit_exception.status_code());
134+
return exit_exception.status_code();
132135
} catch (const std::exception& error) {
133136
// this is the last resort, so using std::cerr and no sdl show_simple messagebox!
134137
std::cerr << error.what();

src/helper/graphic_utils.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
#include "graphic_utils.hpp"
3+
#include <exception>
34

45
SDL_Color utils::sdl_color_from_color(const Color& color) {
56
return SDL_Color{ color.r, color.g, color.b, color.a };
@@ -94,3 +95,25 @@ std::optional<bool> utils::log_error(const std::string& error) {
9495
spdlog::error(error);
9596
return std::nullopt;
9697
}
98+
99+
utils::ExitException::ExitException(int status_code) noexcept : m_status_code{ status_code } { }
100+
101+
[[nodiscard]] int utils::ExitException::status_code() const {
102+
return m_status_code;
103+
}
104+
105+
[[nodiscard]] const char* utils::ExitException::what() const noexcept {
106+
return "An exit exception occurred";
107+
}
108+
109+
void utils::exit(int status_code) {
110+
#if defined(__ANDROID__)
111+
// calling exit() in android doesn't do the correct job, it completely avoids resource cleanup by the underlying SDLActivity.java
112+
// (java wrapper), that calls the main and expects it to return ALWAYS and throwing an exception in a catch statement is bad,
113+
// but is required here
114+
// see: https://github.com/libsdl-org/SDL/blob/main/docs/README-android.md
115+
throw utils::ExitException{ status_code };
116+
#else
117+
std::exit(status_code);
118+
#endif
119+
}

src/helper/graphic_utils.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,20 @@ namespace utils {
2020
[[nodiscard]] std::filesystem::path get_root_folder();
2121

2222
std::optional<bool> log_error(const std::string& error);
23+
24+
struct ExitException : std::exception {
25+
private:
26+
int m_status_code;
27+
28+
public:
29+
explicit ExitException(int status_code) noexcept;
30+
31+
[[nodiscard]] int status_code() const;
32+
33+
[[nodiscard]] const char* what() const noexcept override;
34+
};
35+
36+
37+
[[noreturn]] void exit(int status_code);
38+
2339
} // namespace utils

src/libs/core/helper/errors.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ helper::GeneralError::GeneralError(GeneralError&& error) noexcept = default;
2121
return m_message;
2222
}
2323

24+
[[nodiscard]] const char* helper::GeneralError::what() const noexcept {
25+
return m_message.c_str();
26+
}
27+
28+
2429
[[nodiscard]] helper::error::Severity helper::GeneralError::severity() const {
2530
return m_severity;
2631
}

src/libs/core/helper/errors.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ namespace helper {
3131

3232
[[nodiscard]] const std::string& message() const;
3333
[[nodiscard]] error::Severity severity() const;
34+
35+
[[nodiscard]] const char* what() const noexcept override;
3436
};
3537

3638
struct FatalError : public GeneralError {

0 commit comments

Comments
 (0)