Skip to content
Snippets Groups Projects
Commit 055ff090 authored by Philippe Canal's avatar Philippe Canal
Browse files

Fix Memory leaks when using TPgSQLStatement. See JIRA ROOT-5393

when recently valgrinding our analysis-software again, I found two leaks stemming from lost pointers to prepared PgSQL-statements in TPgSQLStatement (we just started using the TSQLStatement more often some weeks ago).
The problem is that the TPgSQLServer already prepares a PGresult and adds a pointer to it to the stmt-structure which is passed to TPgSQLStatement.
The TPgSQLStatement creates a new prepared statement in its constructor and overrides this pointer with it.
In TPgSQLStatement:Process(), this pointer is overwritten again.

To not break any existing usecases of this, the attached patch fixes this problem by first PQclear'ing the prepared result if it is not NULL. This fixes both leaks (and at the moment, I do not observe more leaks in our usage of the TPgSQLStatement).

[From Oliver Freyermuth]
parent d0549393
No related branches found
No related tags found
No related merge requests found
......@@ -49,6 +49,11 @@ TPgSQLStatement::TPgSQLStatement(PgSQL_Stmt_t* stmt, Bool_t errout):
// Normal constructor.
// Checks if statement contains parameters tags.
// Given fRes not used, we retrieve the statement using the connection.
if (fStmt->fRes != NULL) {
PQclear(fStmt->fRes);
}
fStmt->fRes = PQdescribePrepared(fStmt->fConn,"preparedstmt");
unsigned long paramcount = PQnparams(fStmt->fRes);
fNumResultCols = PQnfields(fStmt->fRes);
......@@ -151,6 +156,12 @@ Bool_t TPgSQLStatement::Process()
CheckStmt("Process",kFALSE);
// We create the prepared statement below, MUST delete the old one
// from our constructor first!
if (fStmt->fRes != NULL) {
PQclear(fStmt->fRes);
}
if (IsSetParsMode()) {
fStmt->fRes= PQexecPrepared(fStmt->fConn,"preparedstmt",fNumBuffers,
(const char* const*)fBind,
......@@ -284,7 +295,7 @@ void TPgSQLStatement::FreeBuffers()
{
// Release all buffers, used by statement.
//individual field names free()'ed by PQClear of fStmt->fRes
//individual field names free()'ed by PQclear of fStmt->fRes
if (fFieldName)
delete[] fFieldName;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment