Use non-static member vars for SDL clipboard / primary selection buffers

This commit is contained in:
DS 2023-03-26 14:13:58 +02:00 committed by GitHub
parent a67f3003de
commit ba1cd19983
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 14 deletions

@ -30,7 +30,7 @@
#include "fast_atof.h" #include "fast_atof.h"
#if defined(_IRR_COMPILE_WITH_SDL_DEVICE_) #if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
static bool sdl_supports_primary_selection = [] { static const bool sdl_supports_primary_selection = [] {
#if SDL_VERSION_ATLEAST(2, 25, 0) #if SDL_VERSION_ATLEAST(2, 25, 0)
SDL_version linked_version; SDL_version linked_version;
SDL_GetVersion(&linked_version); SDL_GetVersion(&linked_version);
@ -62,6 +62,15 @@ COSOperator::COSOperator(const core::stringc& osVersion) : OperatingSystem(osVer
} }
COSOperator::~COSOperator()
{
#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
SDL_free(ClipboardSelectionText);
SDL_free(PrimarySelectionText);
#endif
}
//! returns the current operating system version as string. //! returns the current operating system version as string.
const core::stringc& COSOperator::getOperatingSystemVersion() const const core::stringc& COSOperator::getOperatingSystemVersion() const
{ {
@ -142,11 +151,9 @@ void COSOperator::copyToPrimarySelection(const c8 *text) const
const c8* COSOperator::getTextFromClipboard() const const c8* COSOperator::getTextFromClipboard() const
{ {
#if defined(_IRR_COMPILE_WITH_SDL_DEVICE_) #if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
static char *text = nullptr; SDL_free(ClipboardSelectionText);
if (text) ClipboardSelectionText = SDL_GetClipboardText();
SDL_free(text); return ClipboardSelectionText;
text = SDL_GetClipboardText();
return text;
#elif defined(_IRR_WINDOWS_API_) #elif defined(_IRR_WINDOWS_API_)
if (!OpenClipboard(NULL)) if (!OpenClipboard(NULL))
@ -195,11 +202,9 @@ const c8* COSOperator::getTextFromPrimarySelection() const
#if defined(_IRR_COMPILE_WITH_SDL_DEVICE_) #if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
#if SDL_VERSION_ATLEAST(2, 25, 0) #if SDL_VERSION_ATLEAST(2, 25, 0)
if (sdl_supports_primary_selection) { if (sdl_supports_primary_selection) {
static char *text = nullptr; SDL_free(PrimarySelectionText);
if (text) PrimarySelectionText = SDL_GetPrimarySelectionText();
SDL_free(text); return PrimarySelectionText;
text = SDL_GetPrimarySelectionText();
return text;
} }
#endif #endif
return 0; return 0;

@ -23,6 +23,11 @@ public:
#endif #endif
COSOperator(const core::stringc& osversion); COSOperator(const core::stringc& osversion);
~COSOperator();
COSOperator(const COSOperator &) = delete;
COSOperator &operator=(const COSOperator &) = delete;
//! returns the current operation system version as string. //! returns the current operation system version as string.
const core::stringc& getOperatingSystemVersion() const override; const core::stringc& getOperatingSystemVersion() const override;
@ -56,6 +61,12 @@ private:
mutable core::stringc ClipboardBuf; mutable core::stringc ClipboardBuf;
#endif #endif
#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
// These need to be freed with SDL_free
mutable char *ClipboardSelectionText = nullptr;
mutable char *PrimarySelectionText = nullptr;
#endif
}; };
} // end namespace } // end namespace