From 7079801697b0399cfdefea036ae3690d635eae9a Mon Sep 17 00:00:00 2001 From: David Capello Date: Mon, 15 Mar 2021 21:05:33 -0300 Subject: [PATCH] Avoid leaks running "gen" when memory sanitizer is enabled --- src/gen/LICENSE.txt | 2 +- src/gen/check_strings.cpp | 9 +++++---- src/gen/gen.cpp | 14 ++++++++------ 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/gen/LICENSE.txt b/src/gen/LICENSE.txt index 00a481e95e..18ec883cb2 100644 --- a/src/gen/LICENSE.txt +++ b/src/gen/LICENSE.txt @@ -1,4 +1,4 @@ -Copyright (C) 2019 Igara Studio S.A. +Copyright (C) 2019-2021 Igara Studio S.A. Copyright (C) 2014-2018 David Capello Permission is hereby granted, free of charge, to any person obtaining diff --git a/src/gen/check_strings.cpp b/src/gen/check_strings.cpp index 50d91dc996..720dac9ed0 100644 --- a/src/gen/check_strings.cpp +++ b/src/gen/check_strings.cpp @@ -1,4 +1,5 @@ // Aseprite Code Generator +// Copyright (c) 2021 Igara Studio S.A. // Copyright (c) 2016-2018 David Capello // // This file is released under the terms of the MIT license. @@ -90,9 +91,9 @@ class CheckStrings { void loadStrings(const std::string& dir) { for (const auto& fn : base::list_files(dir)) { - cfg::CfgFile* f = new cfg::CfgFile; + std::unique_ptr f(new cfg::CfgFile); f->load(base::join_path(dir, fn)); - m_stringFiles.push_back(f); + m_stringFiles.push_back(std::move(f)); } } @@ -183,7 +184,7 @@ class CheckStrings { if (!text) return; // Do nothing else if (text[0] == '@') { - for (auto cfg : m_stringFiles) { + for (auto& cfg : m_stringFiles) { std::string lang = base::get_file_title(cfg->filename()); std::string section, var; @@ -222,7 +223,7 @@ class CheckStrings { } private: - std::vector m_stringFiles; + std::vector> m_stringFiles; std::string m_prefixId; }; diff --git a/src/gen/gen.cpp b/src/gen/gen.cpp index 74464ed913..e38fb4483a 100644 --- a/src/gen/gen.cpp +++ b/src/gen/gen.cpp @@ -1,4 +1,5 @@ // Aseprite Code Generator +// Copyright (c) 2021 Igara Studio S.A. // Copyright (c) 2014-2017 David Capello // // This file is released under the terms of the MIT license. @@ -17,6 +18,7 @@ #include "tinyxml.h" #include +#include typedef base::ProgramOptions PO; @@ -36,13 +38,13 @@ static void run(int argc, const char* argv[]) po.parse(argc, argv); // Try to load the XML file - TiXmlDocument* doc = nullptr; + std::unique_ptr doc; std::string inputFilename = po.value_of(inputOpt); if (!inputFilename.empty() && base::get_file_extension(inputFilename) == "xml") { base::FileHandle inputFile(base::open_file(inputFilename, "rb")); - doc = new TiXmlDocument(); + doc.reset(new TiXmlDocument); doc->SetValue(inputFilename.c_str()); if (!doc->LoadFile(inputFile.get())) { std::cerr << doc->Value() << ":" @@ -58,16 +60,16 @@ static void run(int argc, const char* argv[]) if (doc) { // Generate widget class if (po.enabled(widgetId)) - gen_ui_class(doc, inputFilename, po.value_of(widgetId)); + gen_ui_class(doc.get(), inputFilename, po.value_of(widgetId)); // Generate preference header file else if (po.enabled(prefH)) - gen_pref_header(doc, inputFilename); + gen_pref_header(doc.get(), inputFilename); // Generate preference c++ file else if (po.enabled(prefCpp)) - gen_pref_impl(doc, inputFilename); + gen_pref_impl(doc.get(), inputFilename); // Generate theme class else if (po.enabled(theme)) - gen_theme_class(doc, inputFilename); + gen_theme_class(doc.get(), inputFilename); } // Generate strings.ini.h file else if (po.enabled(strings)) {