Skip to content

Commit

Permalink
Avoid leaks running "gen" when memory sanitizer is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
dacap committed Mar 16, 2021
1 parent 9189b8b commit 7079801
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion 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
Expand Down
9 changes: 5 additions & 4 deletions 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.
Expand Down Expand Up @@ -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<cfg::CfgFile> f(new cfg::CfgFile);
f->load(base::join_path(dir, fn));
m_stringFiles.push_back(f);
m_stringFiles.push_back(std::move(f));
}
}

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -222,7 +223,7 @@ class CheckStrings {
}

private:
std::vector<cfg::CfgFile*> m_stringFiles;
std::vector<std::unique_ptr<cfg::CfgFile>> m_stringFiles;
std::string m_prefixId;
};

Expand Down
14 changes: 8 additions & 6 deletions 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.
Expand All @@ -17,6 +18,7 @@
#include "tinyxml.h"

#include <iostream>
#include <memory>

typedef base::ProgramOptions PO;

Expand All @@ -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<TiXmlDocument> 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() << ":"
Expand All @@ -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)) {
Expand Down

0 comments on commit 7079801

Please sign in to comment.