diff --git a/libs/pbd/locale_guard.cc b/libs/pbd/locale_guard.cc index 44b9fc2b64..b8493cf176 100644 --- a/libs/pbd/locale_guard.cc +++ b/libs/pbd/locale_guard.cc @@ -25,24 +25,29 @@ using namespace PBD; -LocaleGuard::LocaleGuard (const char* str) -{ - old = setlocale (LC_NUMERIC, NULL); +// try to avoid calling setlocale() recursively. this is not thread-safe. +std::string PBD::LocaleGuard::current; - if (old) { - old = strdup (old); - if (strcmp (old, str)) { - setlocale (LC_NUMERIC, str); - } - } +LocaleGuard::LocaleGuard (const char* str) + : old(0) +{ + if (current != str) { + old = strdup (setlocale (LC_NUMERIC, NULL)); + if (strcmp (old, str)) { + if (setlocale (LC_NUMERIC, str)) + current = str; + } + } } LocaleGuard::~LocaleGuard () { - setlocale (LC_NUMERIC, old); + if (old) { + if (setlocale (LC_NUMERIC, old)) + current = old; - if (old) { - free (const_cast(old)); - } + free ((char*)old); + } } + diff --git a/libs/pbd/pbd/locale_guard.h b/libs/pbd/pbd/locale_guard.h index cac77ded24..8ef100f3f7 100644 --- a/libs/pbd/pbd/locale_guard.h +++ b/libs/pbd/pbd/locale_guard.h @@ -22,12 +22,16 @@ #include "pbd/libpbd_visibility.h" +#include + namespace PBD { struct LIBPBD_API LocaleGuard { LocaleGuard (const char*); ~LocaleGuard (); const char* old; + + static std::string current; }; }