From c3313d0d418cb2aaaf2226eb0a7729ef567b6afb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 5 Dec 2017 01:05:45 +0100 Subject: Always free the PGresult pointer returned by PQexec Fix a somewhat big memory leak --- src/database/postgresql_engine.cpp | 6 ++++++ src/database/postgresql_statement.hpp | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/database/postgresql_engine.cpp b/src/database/postgresql_engine.cpp index 1c01ed5..4ee4223 100644 --- a/src/database/postgresql_engine.cpp +++ b/src/database/postgresql_engine.cpp @@ -1,6 +1,8 @@ #include #ifdef PQ_FOUND +#include + #include #include @@ -53,6 +55,10 @@ std::tuple PostgresqlEngine::raw_exec(const std::string& quer { log_debug("raw_exec:", query); PGresult* res = PQexec(this->conn, query.data()); + auto sg = utils::make_scope_guard([res](){ + PQclear(res); + }); + auto res_status = PQresultStatus(res); if (res_status != PGRES_COMMAND_OK) return std::make_tuple(false, PQresultErrorMessage(res)); diff --git a/src/database/postgresql_statement.hpp b/src/database/postgresql_statement.hpp index 6e5dec8..30c3ec2 100644 --- a/src/database/postgresql_statement.hpp +++ b/src/database/postgresql_statement.hpp @@ -14,7 +14,10 @@ class PostgresqlStatement: public Statement conn(conn) {} ~PostgresqlStatement() - {} + { + PQclear(this->result); + this->result = nullptr; + } PostgresqlStatement(const PostgresqlStatement&) = delete; PostgresqlStatement& operator=(const PostgresqlStatement&) = delete; PostgresqlStatement(PostgresqlStatement&& other) = delete; -- cgit v1.2.3