Skip to content

Commit

Permalink
TileGenerator: free database resources (#38)
Browse files Browse the repository at this point in the history
Destructor of DB* instance was never called.
Ensure it is, adding missing base class virtual destructor and calling delete when possible to free resources.
  • Loading branch information
Nestorfish authored and sfan5 committed Oct 13, 2016
1 parent d83f0d9 commit 26b6293
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 4 deletions.
11 changes: 10 additions & 1 deletion TileGenerator.cpp
Expand Up @@ -110,7 +110,8 @@ TileGenerator::TileGenerator():
m_shading(true),
m_backend(""),
m_border(0),
m_image(0),
m_db(NULL),
m_image(NULL),
m_xMin(INT_MAX),
m_xMax(INT_MIN),
m_zMin(INT_MAX),
Expand All @@ -127,6 +128,7 @@ TileGenerator::TileGenerator():

TileGenerator::~TileGenerator()
{
closeDatabase();
}

void TileGenerator::setBgColor(const std::string &bgColor)
Expand Down Expand Up @@ -246,6 +248,7 @@ void TileGenerator::generate(const std::string &input, const std::string &output
loadBlocks();
createImage();
renderMap();
closeDatabase();
if (m_drawScale) {
renderScale();
}
Expand Down Expand Up @@ -314,6 +317,12 @@ void TileGenerator::openDb(const std::string &input)
throw std::runtime_error(((std::string) "Unknown map backend: ") + backend);
}

void TileGenerator::closeDatabase()
{
delete m_db;
m_db = NULL;
}

void TileGenerator::loadBlocks()
{
std::vector<BlockPos> vec = m_db->getBlockPos();
Expand Down
1 change: 1 addition & 0 deletions TileGenerator.h
Expand Up @@ -76,6 +76,7 @@ class TileGenerator
private:
void parseColorsStream(std::istream &in);
void openDb(const std::string &input);
void closeDatabase();
void loadBlocks();
void createImage();
void renderMap();
Expand Down
2 changes: 1 addition & 1 deletion db-leveldb.h
Expand Up @@ -9,7 +9,7 @@ class DBLevelDB : public DB {
DBLevelDB(const std::string &mapdir);
virtual std::vector<BlockPos> getBlockPos();
virtual void getBlocksOnZ(std::map<int16_t, BlockList> &blocks, int16_t zPos);
~DBLevelDB();
virtual ~DBLevelDB();
private:
void loadPosCache();

Expand Down
2 changes: 1 addition & 1 deletion db-redis.h
Expand Up @@ -9,7 +9,7 @@ class DBRedis : public DB {
DBRedis(const std::string &mapdir);
virtual std::vector<BlockPos> getBlockPos();
virtual void getBlocksOnZ(std::map<int16_t, BlockList> &blocks, int16_t zPos);
~DBRedis();
virtual ~DBRedis();
private:
void loadPosCache();

Expand Down
2 changes: 1 addition & 1 deletion db-sqlite3.h
Expand Up @@ -9,7 +9,7 @@ class DBSQLite3 : public DB {
DBSQLite3(const std::string &mapdir);
virtual std::vector<BlockPos> getBlockPos();
virtual void getBlocksOnZ(std::map<int16_t, BlockList> &blocks, int16_t zPos);
~DBSQLite3();
virtual ~DBSQLite3();
private:
sqlite3 *db;

Expand Down
1 change: 1 addition & 0 deletions db.h
Expand Up @@ -55,6 +55,7 @@ class DB {
public:
virtual std::vector<BlockPos> getBlockPos() = 0;
virtual void getBlocksOnZ(std::map<int16_t, BlockList> &blocks, int16_t zPos) = 0;
virtual ~DB() {};
};


Expand Down

0 comments on commit 26b6293

Please sign in to comment.