Avoid leaks running "gen" when memory sanitizer is enabled

This commit is contained in:
David Capello 2021-03-15 21:05:33 -03:00
parent 6dccb1986e
commit cdf271af74
3 changed files with 14 additions and 11 deletions

View File

@ -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 Copyright (C) 2014-2018 David Capello
Permission is hereby granted, free of charge, to any person obtaining Permission is hereby granted, free of charge, to any person obtaining

View File

@ -1,4 +1,5 @@
// Aseprite Code Generator // Aseprite Code Generator
// Copyright (c) 2021 Igara Studio S.A.
// Copyright (c) 2016-2018 David Capello // Copyright (c) 2016-2018 David Capello
// //
// This file is released under the terms of the MIT license. // This file is released under the terms of the MIT license.
@ -90,9 +91,9 @@ public:
void loadStrings(const std::string& dir) { void loadStrings(const std::string& dir) {
for (const auto& fn : base::list_files(dir)) { for (const auto& fn : base::list_files(dir)) {
cfg::CfgFile* f = new cfg::CfgFile; std::unique_ptr<cfg::CfgFile> f(new cfg::CfgFile);
f->load(base::join_path(dir, fn)); f->load(base::join_path(dir, fn));
m_stringFiles.push_back(f); m_stringFiles.push_back(std::move(f));
} }
} }
@ -183,7 +184,7 @@ public:
if (!text) if (!text)
return; // Do nothing return; // Do nothing
else if (text[0] == '@') { 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 lang = base::get_file_title(cfg->filename());
std::string section, var; std::string section, var;
@ -222,7 +223,7 @@ public:
} }
private: private:
std::vector<cfg::CfgFile*> m_stringFiles; std::vector<std::unique_ptr<cfg::CfgFile>> m_stringFiles;
std::string m_prefixId; std::string m_prefixId;
}; };

View File

@ -1,4 +1,5 @@
// Aseprite Code Generator // Aseprite Code Generator
// Copyright (c) 2021 Igara Studio S.A.
// Copyright (c) 2014-2017 David Capello // Copyright (c) 2014-2017 David Capello
// //
// This file is released under the terms of the MIT license. // This file is released under the terms of the MIT license.
@ -17,6 +18,7 @@
#include "tinyxml.h" #include "tinyxml.h"
#include <iostream> #include <iostream>
#include <memory>
typedef base::ProgramOptions PO; typedef base::ProgramOptions PO;
@ -36,13 +38,13 @@ static void run(int argc, const char* argv[])
po.parse(argc, argv); po.parse(argc, argv);
// Try to load the XML file // Try to load the XML file
TiXmlDocument* doc = nullptr; std::unique_ptr<TiXmlDocument> doc;
std::string inputFilename = po.value_of(inputOpt); std::string inputFilename = po.value_of(inputOpt);
if (!inputFilename.empty() && if (!inputFilename.empty() &&
base::get_file_extension(inputFilename) == "xml") { base::get_file_extension(inputFilename) == "xml") {
base::FileHandle inputFile(base::open_file(inputFilename, "rb")); base::FileHandle inputFile(base::open_file(inputFilename, "rb"));
doc = new TiXmlDocument(); doc.reset(new TiXmlDocument);
doc->SetValue(inputFilename.c_str()); doc->SetValue(inputFilename.c_str());
if (!doc->LoadFile(inputFile.get())) { if (!doc->LoadFile(inputFile.get())) {
std::cerr << doc->Value() << ":" std::cerr << doc->Value() << ":"
@ -58,16 +60,16 @@ static void run(int argc, const char* argv[])
if (doc) { if (doc) {
// Generate widget class // Generate widget class
if (po.enabled(widgetId)) 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 // Generate preference header file
else if (po.enabled(prefH)) else if (po.enabled(prefH))
gen_pref_header(doc, inputFilename); gen_pref_header(doc.get(), inputFilename);
// Generate preference c++ file // Generate preference c++ file
else if (po.enabled(prefCpp)) else if (po.enabled(prefCpp))
gen_pref_impl(doc, inputFilename); gen_pref_impl(doc.get(), inputFilename);
// Generate theme class // Generate theme class
else if (po.enabled(theme)) else if (po.enabled(theme))
gen_theme_class(doc, inputFilename); gen_theme_class(doc.get(), inputFilename);
} }
// Generate strings.ini.h file // Generate strings.ini.h file
else if (po.enabled(strings)) { else if (po.enabled(strings)) {