Re: How to properly fix memory leak - Mailing list pgsql-general

From Igor Korot
Subject Re: How to properly fix memory leak
Date
Msg-id CA+FnnTzVntnd+RBvNQ57ZkPgKNc7HTjhxvtuELBNVcsSbZut5A@mail.gmail.com
Whole thread Raw
In response to Re: How to properly fix memory leak  (Laurenz Albe <laurenz.albe@cybertec.at>)
Responses Re: How to properly fix memory leak
List pgsql-general
Hi, Lauren’s,

On Sat, Apr 26, 2025 at 3:30 PM Laurenz Albe <laurenz.albe@cybertec.at> wrote:
On Fri, 2025-04-25 at 22:24 -0500, Igor Korot wrote:
> Hi, ALL,
>
> [code]
>     auto res = PQexec( m_db, m_pimpl->m_myconv.to_bytes( query.c_str()
> ).c_str() );      /* ask for binary results */
>     if( PQresultStatus( res ) != PGRES_TUPLES_OK )
>     {
>         auto err = m_pimpl->m_myconv.from_bytes( PQerrorMessage( m_db ) );
>         errorMsg.push_back( L"Update validation table: " + err );
>         result = 1;
>     }
>     else
>     {
>         for( int i = 0; i < PQntuples( res ); i++ )
>         {
>             auto temp1 = m_pimpl->m_myconv.from_bytes( PQgetvalue(
> res, i, 1 ) );
>             m_tablespaces.push_back( temp1 );
>         } // this line gives a leak according to VLD
>     }
>     PQclear( res );
>     return result;
> [/code]
>
> I ran this code on MSVC 2017  with VLD and according to the VLD report I have
> a memory leak on the line indicated.
>
> Should I call PQclear() on every iteration of the loop?
>
> And I hope I handle the error cae properly...

No, PQclear() would cause an error (double free).

If it is not a spurious complaint, the leak would have to be in m_tablespaces.push_back().

No, it is not spurious.
I’m getting it every time I run the program.

The m_tablespaces variable is declared as “std::vector<std::wstring>. No pointer is involved.
I don’t see how it can produce the leak…

Thank you.



Yours,
Laurenz Albe

pgsql-general by date:

Previous
From: Laurenz Albe
Date:
Subject: Re: How to properly fix memory leak
Next
From: Tom Lane
Date:
Subject: Re: How to properly fix memory leak