Always free the PGresult pointer returned by PQexec
Fix a somewhat big memory leak
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
#include <biboumi.h>
|
#include <biboumi.h>
|
||||||
#ifdef PQ_FOUND
|
#ifdef PQ_FOUND
|
||||||
|
|
||||||
|
#include <utils/scopeguard.hpp>
|
||||||
|
|
||||||
#include <database/postgresql_engine.hpp>
|
#include <database/postgresql_engine.hpp>
|
||||||
|
|
||||||
#include <database/postgresql_statement.hpp>
|
#include <database/postgresql_statement.hpp>
|
||||||
@@ -53,6 +55,10 @@ std::tuple<bool, std::string> PostgresqlEngine::raw_exec(const std::string& quer
|
|||||||
{
|
{
|
||||||
log_debug("raw_exec:", query);
|
log_debug("raw_exec:", query);
|
||||||
PGresult* res = PQexec(this->conn, query.data());
|
PGresult* res = PQexec(this->conn, query.data());
|
||||||
|
auto sg = utils::make_scope_guard([res](){
|
||||||
|
PQclear(res);
|
||||||
|
});
|
||||||
|
|
||||||
auto res_status = PQresultStatus(res);
|
auto res_status = PQresultStatus(res);
|
||||||
if (res_status != PGRES_COMMAND_OK)
|
if (res_status != PGRES_COMMAND_OK)
|
||||||
return std::make_tuple(false, PQresultErrorMessage(res));
|
return std::make_tuple(false, PQresultErrorMessage(res));
|
||||||
|
|||||||
@@ -14,7 +14,10 @@ class PostgresqlStatement: public Statement
|
|||||||
conn(conn)
|
conn(conn)
|
||||||
{}
|
{}
|
||||||
~PostgresqlStatement()
|
~PostgresqlStatement()
|
||||||
{}
|
{
|
||||||
|
PQclear(this->result);
|
||||||
|
this->result = nullptr;
|
||||||
|
}
|
||||||
PostgresqlStatement(const PostgresqlStatement&) = delete;
|
PostgresqlStatement(const PostgresqlStatement&) = delete;
|
||||||
PostgresqlStatement& operator=(const PostgresqlStatement&) = delete;
|
PostgresqlStatement& operator=(const PostgresqlStatement&) = delete;
|
||||||
PostgresqlStatement(PostgresqlStatement&& other) = delete;
|
PostgresqlStatement(PostgresqlStatement&& other) = delete;
|
||||||
|
|||||||
Reference in New Issue
Block a user