Merge pull request #525 from kbelyaev/memory_leaks_fix

Memory leaks fix
This commit is contained in:
Thomas Fussell 2021-01-03 19:44:46 -05:00 committed by GitHub
commit e66e417b0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 47 additions and 50 deletions

View File

@ -154,7 +154,7 @@ namespace xlnt {
const std::unordered_map<std::string, int> &cell::error_codes() const std::unordered_map<std::string, int> &cell::error_codes()
{ {
static const auto *codes = new std::unordered_map<std::string, int>{ static const auto codes = std::unordered_map<std::string, int>{
{"#NULL!", 0}, {"#NULL!", 0},
{"#DIV/0!", 1}, {"#DIV/0!", 1},
{"#VALUE!", 2}, {"#VALUE!", 2},
@ -163,7 +163,7 @@ const std::unordered_map<std::string, int> &cell::error_codes()
{"#NUM!", 5}, {"#NUM!", 5},
{"#N/A!", 6}}; {"#N/A!", 6}};
return *codes; return codes;
} }
std::string cell::check_string(const std::string &to_check) std::string cell::check_string(const std::string &to_check)

View File

@ -122,8 +122,8 @@ const path constants::part_shared_strings()
const std::unordered_map<std::string, std::string> &constants::namespaces() const std::unordered_map<std::string, std::string> &constants::namespaces()
{ {
static const std::unordered_map<std::string, std::string> *namespaces = static const std::unordered_map<std::string, std::string> namespaces =
new std::unordered_map<std::string, std::string>{ std::unordered_map<std::string, std::string>{
{"spreadsheetml", "http://schemas.openxmlformats.org/spreadsheetml/2006/main"}, {"spreadsheetml", "http://schemas.openxmlformats.org/spreadsheetml/2006/main"},
{"content-types", "http://schemas.openxmlformats.org/package/2006/content-types"}, {"content-types", "http://schemas.openxmlformats.org/package/2006/content-types"},
{"relationships", "http://schemas.openxmlformats.org/package/2006/relationships"}, {"relationships", "http://schemas.openxmlformats.org/package/2006/relationships"},
@ -157,7 +157,7 @@ const std::unordered_map<std::string, std::string> &constants::namespaces()
{"loext", "http://schemas.libreoffice.org/"}}; {"loext", "http://schemas.libreoffice.org/"}};
return *namespaces; return namespaces;
} }
const std::string &constants::ns(const std::string &id) const std::string &constants::ns(const std::string &id)

View File

@ -42,8 +42,8 @@ using namespace xlnt::detail;
int compare_keys(const std::string &left, const std::string &right) int compare_keys(const std::string &left, const std::string &right)
{ {
auto to_lower = [](std::string s) { auto to_lower = [](std::string s) {
static const auto *locale = new std::locale(); static const auto locale = std::locale();
std::use_facet<std::ctype<char>>(*locale).tolower(&s[0], &s[0] + s.size()); std::use_facet<std::ctype<char>>(locale).tolower(&s[0], &s[0] + s.size());
return s; return s;
}; };

View File

@ -84,7 +84,7 @@ struct stylesheet
class style create_builtin_style(const std::size_t builtin_id) class style create_builtin_style(const std::size_t builtin_id)
{ {
// From Annex G.2 // From Annex G.2
static const auto *names = new std::unordered_map<std::size_t , std::string> static const auto names = std::unordered_map<std::size_t , std::string>
{ {
{ 0, "Normal" }, { 0, "Normal" },
{ 1, "RowLevel_1" }, { 1, "RowLevel_1" },
@ -140,7 +140,7 @@ struct stylesheet
{ 53, "Explanatory Text" } { 53, "Explanatory Text" }
}; };
auto new_style = create_style(names->at(builtin_id)); auto new_style = create_style(names.at(builtin_id));
new_style.d_->builtin_id = builtin_id; new_style.d_->builtin_id = builtin_id;
return new_style; return new_style;

View File

@ -34,7 +34,7 @@ namespace {
const std::unordered_map<int, std::string> known_locales() const std::unordered_map<int, std::string> known_locales()
{ {
static const std::unordered_map<int, std::string> *all = new std::unordered_map<int, std::string>( static const std::unordered_map<int, std::string> all = std::unordered_map<int, std::string>(
{ {
{0x1, "Arabic"}, {0x1, "Arabic"},
{0x2, "Bulgarian"}, {0x2, "Bulgarian"},
@ -468,7 +468,7 @@ const std::unordered_map<int, std::string> known_locales()
{0x7C92, "Central Kurdish (Arabic)"}, {0x7C92, "Central Kurdish (Arabic)"},
}); });
return *all; return all;
} }
[[noreturn]] void unhandled_case_error() [[noreturn]] void unhandled_case_error()
@ -1750,11 +1750,11 @@ std::string number_formatter::fill_fraction_placeholders(const format_placeholde
std::string number_formatter::format_number(const format_code &format, double number) std::string number_formatter::format_number(const format_code &format, double number)
{ {
static const std::vector<std::string> *month_names = new std::vector<std::string>{"January", "February", "March", static const std::vector<std::string> month_names = std::vector<std::string>{"January", "February", "March",
"April", "May", "June", "July", "August", "September", "October", "November", "December"}; "April", "May", "June", "July", "August", "September", "October", "November", "December"};
static const std::vector<std::string> *day_names = static const std::vector<std::string> day_names =
new std::vector<std::string>{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; std::vector<std::string>{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
std::string result; std::string result;
@ -1885,12 +1885,12 @@ std::string number_formatter::format_number(const format_code &format, double nu
} }
case template_part::template_type::month_abbreviation: { case template_part::template_type::month_abbreviation: {
result.append(month_names->at(static_cast<std::size_t>(dt.month) - 1).substr(0, 3)); result.append(month_names.at(static_cast<std::size_t>(dt.month) - 1).substr(0, 3));
break; break;
} }
case template_part::template_type::month_name: { case template_part::template_type::month_name: {
result.append(month_names->at(static_cast<std::size_t>(dt.month) - 1)); result.append(month_names.at(static_cast<std::size_t>(dt.month) - 1));
break; break;
} }
@ -2028,17 +2028,17 @@ std::string number_formatter::format_number(const format_code &format, double nu
} }
case template_part::template_type::month_letter: { case template_part::template_type::month_letter: {
result.append(month_names->at(static_cast<std::size_t>(dt.month) - 1).substr(0, 1)); result.append(month_names.at(static_cast<std::size_t>(dt.month) - 1).substr(0, 1));
break; break;
} }
case template_part::template_type::day_abbreviation: { case template_part::template_type::day_abbreviation: {
result.append(day_names->at(static_cast<std::size_t>(dt.weekday())).substr(0, 3)); result.append(day_names.at(static_cast<std::size_t>(dt.weekday())).substr(0, 3));
break; break;
} }
case template_part::template_type::day_name: { case template_part::template_type::day_name: {
result.append(day_names->at(static_cast<std::size_t>(dt.weekday()))); result.append(day_names.at(static_cast<std::size_t>(dt.weekday())));
break; break;
} }
} }

View File

@ -32,7 +32,7 @@ const std::vector<std::uint8_t> &excel_thumbnail();
const std::vector<std::uint8_t> &excel_thumbnail() const std::vector<std::uint8_t> &excel_thumbnail()
{ {
static const auto *data = new std::vector<std::uint8_t>{ static const auto data = std::vector<std::uint8_t>{
0xff,0xd8,0xff,0xe0,0x00,0x10,0x4a,0x46,0x49,0x46,0x00,0x01,0x01,0x00,0x00,0x48,0x00,0x48,0x00,0x00,0xff,0xe1,0x00,0x80,0x45,0x78,0x69,0x66,0x00,0x00,0x4d,0x4d,0x00 0xff,0xd8,0xff,0xe0,0x00,0x10,0x4a,0x46,0x49,0x46,0x00,0x01,0x01,0x00,0x00,0x48,0x00,0x48,0x00,0x00,0xff,0xe1,0x00,0x80,0x45,0x78,0x69,0x66,0x00,0x00,0x4d,0x4d,0x00
,0x2a,0x00,0x00,0x00,0x08,0x00,0x04,0x01,0x1a,0x00,0x05,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x3e,0x01,0x1b,0x00,0x05,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x46,0x01 ,0x2a,0x00,0x00,0x00,0x08,0x00,0x04,0x01,0x1a,0x00,0x05,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x3e,0x01,0x1b,0x00,0x05,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x46,0x01
,0x28,0x00,0x03,0x00,0x00,0x00,0x01,0x00,0x02,0x00,0x00,0x87,0x69,0x00,0x04,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x4e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x48,0x00 ,0x28,0x00,0x03,0x00,0x00,0x00,0x01,0x00,0x02,0x00,0x00,0x87,0x69,0x00,0x04,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x4e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x48,0x00
@ -449,7 +449,7 @@ const std::vector<std::uint8_t> &excel_thumbnail()
,0x9f,0xec,0x28,0x7c,0xef,0x27,0xce,0xf3,0x3c,0xaf,0x36,0x2f,0x33,0x6e,0xcf,0x31,0x33,0xbe,0x80,0x3f,0xff,0xd9,0x00 ,0x9f,0xec,0x28,0x7c,0xef,0x27,0xce,0xf3,0x3c,0xaf,0x36,0x2f,0x33,0x6e,0xcf,0x31,0x33,0xbe,0x80,0x3f,0xff,0xd9,0x00
}; };
return *data; return data;
} }
} // namespace xlnt } // namespace xlnt

View File

@ -77,7 +77,7 @@ inline bool string_equal(const std::string &lhs, const char (&rhs)[N])
xml::qname &qn(const std::string &namespace_, const std::string &name) xml::qname &qn(const std::string &namespace_, const std::string &name)
{ {
using qname_map = std::unordered_map<std::string, xml::qname>; using qname_map = std::unordered_map<std::string, xml::qname>;
static auto &memo = *new std::unordered_map<std::string, qname_map>(); static auto memo = std::unordered_map<std::string, qname_map>();
auto &ns_memo = memo[namespace_]; auto &ns_memo = memo[namespace_];

View File

@ -1250,7 +1250,7 @@ void xlsx_producer::write_styles(const relationship & /*rel*/)
if (stylesheet.known_fonts_enabled) if (stylesheet.known_fonts_enabled)
{ {
auto is_known_font = [](const font &f) { auto is_known_font = [](const font &f) {
const auto &known_fonts = *new std::vector<font>{ const auto known_fonts = std::vector<font>{
font().name("Calibri").family(2).size(12).color(theme_color(1)).scheme("minor")}; font().name("Calibri").family(2).size(12).color(theme_color(1)).scheme("minor")};
return std::find(known_fonts.begin(), known_fonts.end(), f) != known_fonts.end(); return std::find(known_fonts.begin(), known_fonts.end(), f) != known_fonts.end();
@ -1765,7 +1765,7 @@ void xlsx_producer::write_theme(const relationship &theme_rel)
std::string minor; std::string minor;
}; };
static const auto font_schemes = new std::vector<font_scheme>{{true, "latin", "Calibri Light", "Calibri"}, static const auto font_schemes = std::vector<font_scheme>{{true, "latin", "Calibri Light", "Calibri"},
{true, "ea", "", ""}, {true, "cs", "", ""}, {false, "Jpan", "Yu Gothic Light", "Yu Gothic"}, {true, "ea", "", ""}, {true, "cs", "", ""}, {false, "Jpan", "Yu Gothic Light", "Yu Gothic"},
{false, "Hang", "\xeb\xa7\x91\xec\x9d\x80 \xea\xb3\xa0\xeb\x94\x95", {false, "Hang", "\xeb\xa7\x91\xec\x9d\x80 \xea\xb3\xa0\xeb\x94\x95",
"\xeb\xa7\x91\xec\x9d\x80 \xea\xb3\xa0\xeb\x94\x95"}, "\xeb\xa7\x91\xec\x9d\x80 \xea\xb3\xa0\xeb\x94\x95"},
@ -1793,7 +1793,7 @@ void xlsx_producer::write_theme(const relationship &theme_rel)
{ {
write_start_element(xmlns_a, major ? "majorFont" : "minorFont"); write_start_element(xmlns_a, major ? "majorFont" : "minorFont");
for (const auto &scheme : *font_schemes) for (const auto &scheme : font_schemes)
{ {
const auto scheme_value = major ? scheme.major : scheme.minor; const auto scheme_value = major ? scheme.major : scheme.minor;

View File

@ -94,11 +94,11 @@ border::border()
const std::vector<xlnt::border_side> &border::all_sides() const std::vector<xlnt::border_side> &border::all_sides()
{ {
static auto *sides = new std::vector<xlnt::border_side>{xlnt::border_side::start, xlnt::border_side::end, static auto sides = std::vector<xlnt::border_side>{xlnt::border_side::start, xlnt::border_side::end,
xlnt::border_side::top, xlnt::border_side::bottom, xlnt::border_side::diagonal, xlnt::border_side::vertical, xlnt::border_side::top, xlnt::border_side::bottom, xlnt::border_side::diagonal, xlnt::border_side::vertical,
xlnt::border_side::horizontal}; xlnt::border_side::horizontal};
return *sides; return sides;
} }
optional<border::border_property> border::side(border_side s) const optional<border::border_property> border::side(border_side s) const

View File

@ -36,9 +36,9 @@ namespace {
const std::unordered_map<std::size_t, xlnt::number_format> &builtin_formats() const std::unordered_map<std::size_t, xlnt::number_format> &builtin_formats()
{ {
static std::unordered_map<std::size_t, xlnt::number_format> *formats = nullptr; static std::unordered_map<std::size_t, xlnt::number_format> formats;
if (formats == nullptr) if (formats.size() == 0)
{ {
const std::unordered_map<std::size_t, std::string> format_strings{ const std::unordered_map<std::size_t, std::string> format_strings{
{0, "General"}, {0, "General"},
@ -77,17 +77,14 @@ const std::unordered_map<std::size_t, xlnt::number_format> &builtin_formats()
{48, "##0.0E+0"}, {48, "##0.0E+0"},
{49, "@"}}; {49, "@"}};
formats = new std::unordered_map<std::size_t, xlnt::number_format>();
auto &formats_ref = *formats;
for (auto format_string_pair : format_strings) for (auto format_string_pair : format_strings)
{ {
formats_ref[format_string_pair.first] = formats[format_string_pair.first] =
xlnt::number_format(format_string_pair.second, format_string_pair.first); xlnt::number_format(format_string_pair.second, format_string_pair.first);
} }
} }
return *formats; return formats;
} }
} // namespace } // namespace
@ -131,44 +128,44 @@ const number_format number_format::percentage_00()
const number_format number_format::date_yyyymmdd2() const number_format number_format::date_yyyymmdd2()
{ {
static const number_format *format = new number_format("yyyy-mm-dd"); static const number_format format = number_format("yyyy-mm-dd");
return *format; return format;
} }
const number_format number_format::date_yymmdd() const number_format number_format::date_yymmdd()
{ {
static const number_format *format = new number_format("yy-mm-dd"); static const number_format format = number_format("yy-mm-dd");
return *format; return format;
} }
const number_format number_format::date_ddmmyyyy() const number_format number_format::date_ddmmyyyy()
{ {
static const number_format *format = new number_format("dd/mm/yy"); static const number_format format = number_format("dd/mm/yy");
return *format; return format;
} }
const number_format number_format::date_dmyslash() const number_format number_format::date_dmyslash()
{ {
static const number_format *format = new number_format("d/m/yy"); static const number_format format = number_format("d/m/yy");
return *format; return format;
} }
const number_format number_format::date_dmyminus() const number_format number_format::date_dmyminus()
{ {
static const number_format *format = new number_format("d-m-yy"); static const number_format format = number_format("d-m-yy");
return *format; return format;
} }
const number_format number_format::date_dmminus() const number_format number_format::date_dmminus()
{ {
static const number_format *format = new number_format("d-m"); static const number_format format = number_format("d-m");
return *format; return format;
} }
const number_format number_format::date_myminus() const number_format number_format::date_myminus()
{ {
static const number_format *format = new number_format("m-yy"); static const number_format format = number_format("m-yy");
return *format; return format;
} }
const number_format number_format::date_xlsx14() const number_format number_format::date_xlsx14()
@ -198,8 +195,8 @@ const number_format number_format::date_xlsx22()
const number_format number_format::date_datetime() const number_format number_format::date_datetime()
{ {
static const number_format *format = new number_format("yyyy-mm-dd h:mm:ss"); static const number_format format = number_format("yyyy-mm-dd h:mm:ss");
return *format; return format;
} }
const number_format number_format::date_time1() const number_format number_format::date_time1()