From a362e86da130db3698cacd63629d8dcfa48511c7 Mon Sep 17 00:00:00 2001 From: Pedro Date: Wed, 8 Feb 2017 03:54:38 +0100 Subject: [PATCH] resolved memory leaks triggered in select tests --- src/sql/Expr.cpp | 5 +++-- src/sql/statements.cpp | 21 +++++++++++++++++---- test/lib/helper.h | 22 +++++++++++----------- test/lib/select.cpp | 37 +++++++++++++++++++++++++++++++++---- test/lib/test.cpp | 8 ++++---- test/lib/test.h | 6 ++++-- 6 files changed, 72 insertions(+), 27 deletions(-) diff --git a/src/sql/Expr.cpp b/src/sql/Expr.cpp index 07bb1b2..5ced91b 100644 --- a/src/sql/Expr.cpp +++ b/src/sql/Expr.cpp @@ -16,8 +16,9 @@ namespace hsql { Expr::~Expr() { delete expr; delete expr2; - delete name; - delete table; + free(name); + free(table); + free(alias); } Expr* Expr::makeOpUnary(OperatorType op, Expr* expr) { diff --git a/src/sql/statements.cpp b/src/sql/statements.cpp index 4eae74c..d53e7a6 100644 --- a/src/sql/statements.cpp +++ b/src/sql/statements.cpp @@ -142,8 +142,14 @@ namespace hsql { having(NULL) {} GroupByDescription::~GroupByDescription() { - delete columns; delete having; + + if (columns != NULL) { + for (Expr* expr : *columns) { + delete expr; + } + delete columns; + } } // SelectStatement @@ -160,11 +166,18 @@ namespace hsql { SelectStatement::~SelectStatement() { delete fromTable; - delete selectList; delete whereClause; delete groupBy; delete order; delete limit; + + // Delete each element in the select list. + if (selectList != NULL) { + for (Expr* expr : *selectList) { + delete expr; + } + delete selectList; + } } // UpdateStatement @@ -191,8 +204,8 @@ namespace hsql { join(NULL) {} TableRef::~TableRef() { - delete name; - delete alias; + free(name); + free(alias); delete select; delete list; } diff --git a/test/lib/helper.h b/test/lib/helper.h index 143e725..bf3a703 100644 --- a/test/lib/helper.h +++ b/test/lib/helper.h @@ -2,21 +2,21 @@ #define __HELPER_H__ -#define TEST_PARSE_SQL_QUERY(query, outputVar, numStatements) \ - const SQLParserResult* outputVar = SQLParser::parseSQLString(query); \ - ASSERT(outputVar->isValid()); \ - ASSERT_EQ(outputVar->size(), numStatements); +#define TEST_PARSE_SQL_QUERY(query, result, numStatements) \ + const SQLParserResult* result = SQLParser::parseSQLString(query); \ + ASSERT(result->isValid()); \ + ASSERT_EQ(result->size(), numStatements); -#define TEST_PARSE_SINGLE_SQL(query, stmtType, stmtClass, outputVar) \ - TEST_PARSE_SQL_QUERY(query, stmt_list, 1); \ - ASSERT_EQ(stmt_list->getStatement(0)->type(), stmtType); \ - const stmtClass* outputVar = (const stmtClass*) stmt_list->getStatement(0); +#define TEST_PARSE_SINGLE_SQL(query, stmtType, stmtClass, result, outputVar) \ + TEST_PARSE_SQL_QUERY(query, result, 1); \ + ASSERT_EQ(result->getStatement(0)->type(), stmtType); \ + const stmtClass* outputVar = (const stmtClass*) result->getStatement(0); -#define TEST_CAST_STMT(stmt_list, stmt_index, stmtType, stmtClass, outputVar) \ - ASSERT_EQ(stmt_list->getStatement(stmt_index)->type(), stmtType); \ - const stmtClass* outputVar = (const stmtClass*) stmt_list->getStatement(stmt_index); +#define TEST_CAST_STMT(result, stmt_index, stmtType, stmtClass, outputVar) \ + ASSERT_EQ(result->getStatement(stmt_index)->type(), stmtType); \ + const stmtClass* outputVar = (const stmtClass*) result->getStatement(stmt_index); #endif diff --git a/test/lib/select.cpp b/test/lib/select.cpp index 2fc4767..bf5adab 100644 --- a/test/lib/select.cpp +++ b/test/lib/select.cpp @@ -7,15 +7,28 @@ using namespace hsql; TEST(SelectTest) { - TEST_PARSE_SINGLE_SQL("SELECT * FROM students;", kStmtSelect, SelectStatement, stmt); + TEST_PARSE_SINGLE_SQL( + "SELECT * FROM students;", + kStmtSelect, + SelectStatement, + result, + stmt); ASSERT_NULL(stmt->whereClause); ASSERT_NULL(stmt->groupBy); + + delete result; } TEST(SelectHavingTest) { - TEST_PARSE_SINGLE_SQL("SELECT city, AVG(grade) AS avg_grade FROM students GROUP BY city HAVING AVG(grade) < 2.0", kStmtSelect, SelectStatement, stmt); + TEST_PARSE_SINGLE_SQL( + "SELECT city, AVG(grade) AS avg_grade FROM students GROUP BY city HAVING AVG(grade) < 2.0", + kStmtSelect, + SelectStatement, + result, + stmt); + ASSERT_FALSE(stmt->selectDistinct); GroupByDescription* group = stmt->groupBy; @@ -24,23 +37,39 @@ TEST(SelectHavingTest) { ASSERT(group->having->isSimpleOp('<')); ASSERT(group->having->expr->isType(kExprFunctionRef)); ASSERT(group->having->expr2->isType(kExprLiteralFloat)); + + delete result; } TEST(SelectDistinctTest) { - TEST_PARSE_SINGLE_SQL("SELECT DISTINCT grade, city FROM students;", kStmtSelect, SelectStatement, stmt); + TEST_PARSE_SINGLE_SQL( + "SELECT DISTINCT grade, city FROM students;", + kStmtSelect, + SelectStatement, + result, + stmt); ASSERT(stmt->selectDistinct); ASSERT_NULL(stmt->whereClause); + + delete result; } TEST(SelectGroupDistinctTest) { - TEST_PARSE_SINGLE_SQL("SELECT city, COUNT(name), COUNT(DISTINCT grade) FROM students GROUP BY city;", kStmtSelect, SelectStatement, stmt); + TEST_PARSE_SINGLE_SQL( + "SELECT city, COUNT(name), COUNT(DISTINCT grade) FROM students GROUP BY city;", + kStmtSelect, + SelectStatement, + result, + stmt); ASSERT_FALSE(stmt->selectDistinct); ASSERT_EQ(stmt->selectList->size(), 3); ASSERT(!stmt->selectList->at(1)->distinct); ASSERT(stmt->selectList->at(2)->distinct); + + delete result; } diff --git a/test/lib/test.cpp b/test/lib/test.cpp index b39f8fc..2c37650 100644 --- a/test/lib/test.cpp +++ b/test/lib/test.cpp @@ -8,13 +8,13 @@ class TestsManager { // http://www.parashift.com/c++-faq-lite/static-init-order-on-first-use.html public: static std::vector& testNames() { - static std::vector* _testNames = new std::vector; - return *_testNames; + static std::vector testNames; + return testNames; } static std::vector& tests() { - static std::vector* tests = new std::vector; - return *tests; + static std::vector tests; + return tests; } }; diff --git a/test/lib/test.h b/test/lib/test.h index 19fe70e..a9c051f 100644 --- a/test/lib/test.h +++ b/test/lib/test.h @@ -7,8 +7,10 @@ #define TEST(name) \ - void name(); \ - namespace g_dummy { int _##name = AddTest(name, #name); } \ + void name();\ + namespace g_dummy {\ + int _##name = AddTest(name, #name);\ + }\ void name()