-
-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix memory leaks and code quality in autotile code (#2591)
Fixes various code quality related issues in autotile code. Also fixes all memory leaks found by AddressSanitizer. Game doesn't use heap allocation anymore where it's not neccessary and uses smart pointers where possible/required.
- Loading branch information
Showing
7 changed files
with
60 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
// SuperTux | ||
// Copyright (C) 2008-2020 A. Semphris <[email protected]>, | ||
// Matthias Braun <[email protected]>, | ||
// Ingo Ruhnke <[email protected]> | ||
// Copyright (C) 2020 A. Semphris <[email protected]> | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
|
@@ -29,7 +27,7 @@ | |
#include "util/reader_mapping.hpp" | ||
#include "util/file_system.hpp" | ||
|
||
AutotileParser::AutotileParser(std::vector<AutotileSet*>* autotilesets, const std::string& filename) : | ||
AutotileParser::AutotileParser(std::vector<std::unique_ptr<AutotileSet>>& autotilesets, const std::string& filename) : | ||
m_autotilesets(autotilesets), | ||
m_filename(filename), | ||
m_tiles_path() | ||
|
@@ -71,7 +69,7 @@ AutotileParser::parse() | |
void | ||
AutotileParser::parse_autotileset(const ReaderMapping& reader, bool corner) | ||
{ | ||
std::vector<Autotile*>* autotiles = new std::vector<Autotile*>(); | ||
std::vector<Autotile*> autotiles; | ||
|
||
std::string name = "[unnamed]"; | ||
if (!reader.get("name", name)) | ||
|
@@ -91,28 +89,28 @@ AutotileParser::parse_autotileset(const ReaderMapping& reader, bool corner) | |
if (iter.get_key() == "autotile") | ||
{ | ||
ReaderMapping tile_mapping = iter.as_mapping(); | ||
autotiles->push_back(parse_autotile(tile_mapping, corner)); | ||
autotiles.push_back(parse_autotile(tile_mapping, corner)); | ||
} | ||
else if (iter.get_key() != "name" && iter.get_key() != "default") | ||
{ | ||
log_warning << "Unknown symbol '" << iter.get_key() << "' in autotile config file" << std::endl; | ||
} | ||
} | ||
|
||
AutotileSet* autotileset = new AutotileSet(*autotiles, default_id, name, corner); | ||
std::unique_ptr<AutotileSet> autotileset = std::make_unique<AutotileSet>(autotiles, default_id, name, corner); | ||
|
||
if (g_config->developer_mode) | ||
{ | ||
autotileset->validate(); | ||
} | ||
|
||
m_autotilesets->push_back(autotileset); | ||
m_autotilesets.push_back(std::move(autotileset)); | ||
} | ||
|
||
Autotile* | ||
AutotileParser::parse_autotile(const ReaderMapping& reader, bool corner) | ||
{ | ||
std::vector<AutotileMask*> autotile_masks; | ||
std::vector<AutotileMask> autotile_masks; | ||
std::vector<std::pair<uint32_t, float>> alt_ids; | ||
|
||
uint32_t tile_id; | ||
|
@@ -143,11 +141,11 @@ AutotileParser::parse_autotile(const ReaderMapping& reader, bool corner) | |
|
||
if (corner) | ||
{ | ||
parse_mask_corner(mask, &autotile_masks); | ||
parse_mask_corner(mask, autotile_masks); | ||
} | ||
else | ||
{ | ||
parse_mask(mask, &autotile_masks, solid); | ||
parse_mask(mask, autotile_masks, solid); | ||
} | ||
} | ||
else if (iter.get_key() == "alt-id") | ||
|
@@ -177,11 +175,11 @@ AutotileParser::parse_autotile(const ReaderMapping& reader, bool corner) | |
} | ||
} | ||
|
||
return new Autotile(tile_id, alt_ids, autotile_masks, !!solid); | ||
return new Autotile(tile_id, alt_ids, autotile_masks, solid); | ||
} | ||
|
||
void | ||
AutotileParser::parse_mask(std::string mask, std::vector<AutotileMask*>* autotile_masks, bool solid) | ||
AutotileParser::parse_mask(std::string mask, std::vector<AutotileMask>& autotile_masks, bool solid) | ||
{ | ||
if (mask.size() != 8) | ||
{ | ||
|
@@ -224,12 +222,12 @@ AutotileParser::parse_mask(std::string mask, std::vector<AutotileMask*>* autotil | |
|
||
for (uint8_t val : masks) | ||
{ | ||
autotile_masks->push_back(new AutotileMask(val, solid)); | ||
autotile_masks.push_back(AutotileMask(val, solid)); | ||
} | ||
} | ||
|
||
void | ||
AutotileParser::parse_mask_corner(std::string mask, std::vector<AutotileMask*>* autotile_masks) | ||
AutotileParser::parse_mask_corner(std::string mask, std::vector<AutotileMask>& autotile_masks) | ||
{ | ||
if (mask.size() != 4) | ||
{ | ||
|
@@ -272,7 +270,7 @@ AutotileParser::parse_mask_corner(std::string mask, std::vector<AutotileMask*>* | |
|
||
for (uint8_t val : masks) | ||
{ | ||
autotile_masks->push_back(new AutotileMask(val, true)); | ||
autotile_masks.push_back(AutotileMask(val, true)); | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.