From d2874f2e346eafbc00ba1c0ad6a1fb49600f638e Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Fri, 4 Jun 2021 21:37:49 +0200 Subject: [PATCH] Fixed a memory leak during locales switching on macOS and removed frequent locales switching during gcode processing --- src/libslic3r/GCode.cpp | 3 ++- src/libslic3r/GCode/GCodeProcessor.cpp | 2 ++ src/libslic3r/GCodeReader.cpp | 5 +++-- src/libslic3r/LocalesUtils.cpp | 11 ++++------- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index ad39eb41e..ca76a2320 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -720,6 +720,8 @@ void GCode::do_export(Print* print, const char* path, GCodeProcessor::Result* re { PROFILE_CLEAR(); + CNumericLocalesSetter locales_setter; + // Does the file exist? If so, we hope that it is still valid. if (print->is_step_done(psGCodeExport) && boost::filesystem::exists(boost::filesystem::path(path))) return; @@ -750,7 +752,6 @@ void GCode::do_export(Print* print, const char* path, GCodeProcessor::Result* re std::string path_tmp(path); path_tmp += ".tmp"; - CNumericLocalesSetter c_locales_setter; FILE *file = boost::nowide::fopen(path_tmp.c_str(), "wb"); if (file == nullptr) throw Slic3r::RuntimeError(std::string("G-code export to ") + path + " failed.\nCannot open the file for writing.\n"); diff --git a/src/libslic3r/GCode/GCodeProcessor.cpp b/src/libslic3r/GCode/GCodeProcessor.cpp index 1e302c656..1179f7391 100644 --- a/src/libslic3r/GCode/GCodeProcessor.cpp +++ b/src/libslic3r/GCode/GCodeProcessor.cpp @@ -1257,6 +1257,8 @@ void GCodeProcessor::process_file(const std::string& filename, bool apply_postpr { auto last_cancel_callback_time = std::chrono::high_resolution_clock::now(); + CNumericLocalesSetter locales_setter; + #if ENABLE_GCODE_VIEWER_STATISTICS auto start_time = std::chrono::high_resolution_clock::now(); #endif // ENABLE_GCODE_VIEWER_STATISTICS diff --git a/src/libslic3r/GCodeReader.cpp b/src/libslic3r/GCodeReader.cpp index 5ab1ae50c..efc9ace6d 100644 --- a/src/libslic3r/GCodeReader.cpp +++ b/src/libslic3r/GCodeReader.cpp @@ -27,7 +27,8 @@ void GCodeReader::apply_config(const DynamicPrintConfig &config) const char* GCodeReader::parse_line_internal(const char *ptr, GCodeLine &gline, std::pair &command) { PROFILE_FUNC(); - CNumericLocalesSetter locales_setter; // for strtod + + assert(is_decimal_separator_point()); // command and args const char *c = ptr; @@ -153,7 +154,7 @@ bool GCodeReader::GCodeLine::has(char axis) const bool GCodeReader::GCodeLine::has_value(char axis, float &value) const { - CNumericLocalesSetter locales_setter; // for strtod + assert(is_decimal_separator_point()); const char *c = m_raw.c_str(); // Skip the whitespaces. c = skip_whitespaces(c); diff --git a/src/libslic3r/LocalesUtils.cpp b/src/libslic3r/LocalesUtils.cpp index cb118f99c..8f4d26642 100644 --- a/src/libslic3r/LocalesUtils.cpp +++ b/src/libslic3r/LocalesUtils.cpp @@ -11,11 +11,14 @@ CNumericLocalesSetter::CNumericLocalesSetter() _configthreadlocale(_ENABLE_PER_THREAD_LOCALE); m_orig_numeric_locale = std::setlocale(LC_NUMERIC, nullptr); std::setlocale(LC_NUMERIC, "C"); -#else +#elif __linux__ m_original_locale = uselocale((locale_t)0); m_new_locale = duplocale(m_original_locale); m_new_locale = newlocale(LC_NUMERIC_MASK, "C", m_new_locale); uselocale(m_new_locale); +#else // APPLE + m_original_locale = uselocale((locale_t)0); + m_new_locale = newlocale(LC_NUMERIC_MASK, "C", m_original_locale); #endif } @@ -58,7 +61,6 @@ double string_to_double_decimal_point(const std::string& str, size_t* pos /* = n std::string float_to_string_decimal_point(double value, int precision/* = -1*/) { - assert(is_decimal_separator_point()); std::stringstream buf; if (precision >= 0) buf << std::fixed << std::setprecision(precision); @@ -66,11 +68,6 @@ std::string float_to_string_decimal_point(double value, int precision/* = -1*/) return buf.str(); } -//std::string float_to_string_decimal_point(float value, int precision/* = -1*/) -//{ -// return float_to_string_decimal_point(double(value), precision); -//} - } // namespace Slic3r