From 215046d8b49b51f2c921a1313fd71113ccc9c31f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=9A=93?= Date: Sat, 10 May 2025 10:47:19 -0400 Subject: [PATCH 1/2] Only use `extern` in embedtool.cpp if language is C++ In C, variables defined as `const` have external linkage. In C++, variables defined as `const` have internal linkage, and we need to define them as `extern const` for them to have external linkage like in C. However, in C, defining a variable as `extern const` throws a compiler error, so we need to add `extern` only for C++ files. --- tools/embedtool.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/embedtool.cpp b/tools/embedtool.cpp index caa08df8..17bd3a15 100644 --- a/tools/embedtool.cpp +++ b/tools/embedtool.cpp @@ -41,7 +41,7 @@ int main(int argc, char **argv) { return 3; } - outputf << "#include \n#include \nextern const uint8_t " << arrayname << "[] = {"; + outputf << "#ifndef MKXPZ_EMBEDTOOL_" << arrayname << "\n#define MKXPZ_EMBEDTOOL_" << arrayname << "\n#include \n#include \n#ifdef __cplusplus\nextern\n#endif /* __cplusplus */\nconst uint8_t " << arrayname << "[] = {"; uint64_t len = 0; for (;;) { @@ -53,7 +53,7 @@ int main(int argc, char **argv) { ++len; } - outputf << "};\nextern const size_t " << arrayname << "_len = " << len << "ULL;"; + outputf << "};\n#ifdef __cplusplus\nextern\n#endif /* __cplusplus */\nconst size_t " << arrayname << "_len = " << len << "ULL;\n#endif /* MKXPZ_EMBEDTOOL_" << arrayname << " */\n"; return 0; } From 95f0f08c877e28ddc6742dae8ff680a6a8ba2339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=9A=93?= Date: Sat, 10 May 2025 10:47:48 -0400 Subject: [PATCH 2/2] Fix memory leak in `Input::getClipboardText()` --- src/input/input.cpp | 15 +++++++++------ src/input/input.h | 4 ++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/input/input.cpp b/src/input/input.cpp index 832569e1..f7185018 100644 --- a/src/input/input.cpp +++ b/src/input/input.cpp @@ -32,6 +32,7 @@ #include #include +#include #include #include #include @@ -677,6 +678,8 @@ struct InputPrivate /* Collective binding array */ std::vector bindings; + std::string clipboardText; + ButtonState stateArray[BUTTON_CODE_COUNT*2]; ButtonState *states; @@ -1511,15 +1514,15 @@ void Input::clearText() shState->eThread().textInputBuffer.clear(); } -char *Input::getClipboardText() +const char *Input::getClipboardText() { - char *tx = SDL_GetClipboardText(); - if (!tx) - throw Exception(Exception::SDLError, "Failed to get clipboard text: %s", SDL_GetError()); - return tx; + const char *tx = SDL_GetClipboardText(); + p->clipboardText = tx; + SDL_free((void *)tx); + return p->clipboardText.c_str(); } -void Input::setClipboardText(char *text) +void Input::setClipboardText(const char *text) { if (SDL_SetClipboardText(text) < 0) throw Exception(Exception::SDLError, "Failed to set clipboard text: %s", SDL_GetError()); diff --git a/src/input/input.h b/src/input/input.h index a6376a9e..afe41a5f 100644 --- a/src/input/input.h +++ b/src/input/input.h @@ -110,8 +110,8 @@ public: const char *getText(); void clearText(); - char *getClipboardText(); - void setClipboardText(char *text); + const char *getClipboardText(); + void setClipboardText(const char *text); const char *getAxisName(SDL_GameControllerAxis axis); const char *getButtonName(SDL_GameControllerButton button);