From 26b62933eda625d35b0ba32f6e2e681d6e1db396 Mon Sep 17 00:00:00 2001 From: Nestorfish Date: Thu, 13 Oct 2016 23:26:59 +0200 Subject: [PATCH] TileGenerator: free database resources (#38) Destructor of DB* instance was never called. Ensure it is, adding missing base class virtual destructor and calling delete when possible to free resources. --- TileGenerator.cpp | 11 ++++++++++- TileGenerator.h | 1 + db-leveldb.h | 2 +- db-redis.h | 2 +- db-sqlite3.h | 2 +- db.h | 1 + 6 files changed, 15 insertions(+), 4 deletions(-) diff --git a/TileGenerator.cpp b/TileGenerator.cpp index 26e91f5..7d34ddd 100644 --- a/TileGenerator.cpp +++ b/TileGenerator.cpp @@ -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), @@ -127,6 +128,7 @@ TileGenerator::TileGenerator(): TileGenerator::~TileGenerator() { + closeDatabase(); } void TileGenerator::setBgColor(const std::string &bgColor) @@ -246,6 +248,7 @@ void TileGenerator::generate(const std::string &input, const std::string &output loadBlocks(); createImage(); renderMap(); + closeDatabase(); if (m_drawScale) { renderScale(); } @@ -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 vec = m_db->getBlockPos(); diff --git a/TileGenerator.h b/TileGenerator.h index 6930cf7..34e83c6 100644 --- a/TileGenerator.h +++ b/TileGenerator.h @@ -76,6 +76,7 @@ public: private: void parseColorsStream(std::istream &in); void openDb(const std::string &input); + void closeDatabase(); void loadBlocks(); void createImage(); void renderMap(); diff --git a/db-leveldb.h b/db-leveldb.h index 8128ee9..e2a0e10 100644 --- a/db-leveldb.h +++ b/db-leveldb.h @@ -9,7 +9,7 @@ public: DBLevelDB(const std::string &mapdir); virtual std::vector getBlockPos(); virtual void getBlocksOnZ(std::map &blocks, int16_t zPos); - ~DBLevelDB(); + virtual ~DBLevelDB(); private: void loadPosCache(); diff --git a/db-redis.h b/db-redis.h index 2663a9c..0c05824 100644 --- a/db-redis.h +++ b/db-redis.h @@ -9,7 +9,7 @@ public: DBRedis(const std::string &mapdir); virtual std::vector getBlockPos(); virtual void getBlocksOnZ(std::map &blocks, int16_t zPos); - ~DBRedis(); + virtual ~DBRedis(); private: void loadPosCache(); diff --git a/db-sqlite3.h b/db-sqlite3.h index 013035b..80fe07b 100644 --- a/db-sqlite3.h +++ b/db-sqlite3.h @@ -9,7 +9,7 @@ public: DBSQLite3(const std::string &mapdir); virtual std::vector getBlockPos(); virtual void getBlocksOnZ(std::map &blocks, int16_t zPos); - ~DBSQLite3(); + virtual ~DBSQLite3(); private: sqlite3 *db; diff --git a/db.h b/db.h index aaf43ac..c848b71 100644 --- a/db.h +++ b/db.h @@ -55,6 +55,7 @@ protected: public: virtual std::vector getBlockPos() = 0; virtual void getBlocksOnZ(std::map &blocks, int16_t zPos) = 0; + virtual ~DB() {}; };