public inbox for [email protected]help / color / mirror / Atom feed
Re: How to properly fix memory leak 5+ messages / 2 participants [nested] [flat]
* Re: How to properly fix memory leak @ 2025-04-26 03:48 David G. Johnston <[email protected]> 2025-04-26 04:17 ` Re: How to properly fix memory leak Igor Korot <[email protected]> 2025-04-27 04:58 ` Re: How to properly fix memory leak Igor Korot <[email protected]> 0 siblings, 2 replies; 5+ messages in thread From: David G. Johnston @ 2025-04-26 03:48 UTC (permalink / raw) To: Igor Korot <[email protected]>; +Cc: pgsql-generallists.postgresql.org <[email protected]> On Friday, April 25, 2025, Igor Korot <[email protected]> wrote: > > 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. Seems like a false positive. > > Should I call PQclear() on every iteration of the loop? > Would make processing more than a single row impossible if you throw away the result after processing one row. David J. ^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: How to properly fix memory leak 2025-04-26 03:48 Re: How to properly fix memory leak David G. Johnston <[email protected]> @ 2025-04-26 04:17 ` Igor Korot <[email protected]> 2025-04-26 04:55 ` How to properly fix memory leak David G. Johnston <[email protected]> 1 sibling, 1 reply; 5+ messages in thread From: Igor Korot @ 2025-04-26 04:17 UTC (permalink / raw) To: David G. Johnston <[email protected]>; +Cc: pgsql-generallists.postgresql.org <[email protected]> Hi, David, On Fri, Apr 25, 2025 at 10:48 PM David G. Johnston <[email protected]> wrote: > > On Friday, April 25, 2025, Igor Korot <[email protected]> wrote: >> >> >> 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. > > > Seems like a false positive. And the error case was handled correctly, right? Thank you. > >> >> >> Should I call PQclear() on every iteration of the loop? > > > Would make processing more than a single row impossible if you throw away the result after processing one row. > > David J. > ^ permalink raw reply [nested|flat] 5+ messages in thread
* How to properly fix memory leak 2025-04-26 03:48 Re: How to properly fix memory leak David G. Johnston <[email protected]> 2025-04-26 04:17 ` Re: How to properly fix memory leak Igor Korot <[email protected]> @ 2025-04-26 04:55 ` David G. Johnston <[email protected]> 2025-04-26 05:12 ` Re: How to properly fix memory leak Igor Korot <[email protected]> 0 siblings, 1 reply; 5+ messages in thread From: David G. Johnston @ 2025-04-26 04:55 UTC (permalink / raw) To: Igor Korot <[email protected]>; +Cc: pgsql-generallists.postgresql.org <[email protected]> On Friday, April 25, 2025, Igor Korot <[email protected]> wrote: > > And the error case was handled correctly, right? > Seems like answering that requires knowing what the query is or can be. I also have no idea what idiomatic code looks like. Though, I’d probably use PQresultErrorMessage and check affirmatively for the tuples and error cases and have a final else should the status be something unexpected. David J. ^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: How to properly fix memory leak 2025-04-26 03:48 Re: How to properly fix memory leak David G. Johnston <[email protected]> 2025-04-26 04:17 ` Re: How to properly fix memory leak Igor Korot <[email protected]> 2025-04-26 04:55 ` How to properly fix memory leak David G. Johnston <[email protected]> @ 2025-04-26 05:12 ` Igor Korot <[email protected]> 0 siblings, 0 replies; 5+ messages in thread From: Igor Korot @ 2025-04-26 05:12 UTC (permalink / raw) To: David G. Johnston <[email protected]>; +Cc: pgsql-generallists.postgresql.org <[email protected]> Hi, David, On Fri, Apr 25, 2025 at 11:55 PM David G. Johnston <[email protected]> wrote: > > On Friday, April 25, 2025, Igor Korot <[email protected]> wrote: >> >> >> And the error case was handled correctly, right? > > > Seems like answering that requires knowing what the query is or can be. I also have no idea what idiomatic code looks like. Though, I’d probably use PQresultErrorMessage and check affirmatively for the tuples and error cases and have a final else should the status be something unexpected. Understood. Below is the full function: [code] int PostgresDatabase::PopulateTablespaces(std::vector<std::wstring> &errorMsg) { int result = 0; std::wstring errorMessage; std::wstring query = L"SELECT * FROM pg_tablespace;"; 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 ); } } PQclear( res ); return result; } [/code] Thank you. > > David J. > ^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: How to properly fix memory leak 2025-04-26 03:48 Re: How to properly fix memory leak David G. Johnston <[email protected]> @ 2025-04-27 04:58 ` Igor Korot <[email protected]> 1 sibling, 0 replies; 5+ messages in thread From: Igor Korot @ 2025-04-27 04:58 UTC (permalink / raw) To: David G. Johnston <[email protected]>; +Cc: pgsql-generallists.postgresql.org <[email protected]> David et al, On Fri, Apr 25, 2025 at 10:48 PM David G. Johnston <[email protected]> wrote: > > On Friday, April 25, 2025, Igor Korot <[email protected]> wrote: >> >> >> 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. > > > Seems like a false positive. Looks like it is false positive. I ran the code under valgrind and there I didn't get such a leak. I did however get a different issues which I fixed. But even after moving the fixes ober to Windows and trying to run it - I still see that. For now I put this to bed as it is not an issue on Linux. Thank you. > >> >> >> Should I call PQclear() on every iteration of the loop? > > > Would make processing more than a single row impossible if you throw away the result after processing one row. > > David J. > ^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2025-04-27 04:58 UTC | newest] Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2025-04-26 03:48 Re: How to properly fix memory leak David G. Johnston <[email protected]> 2025-04-26 04:17 ` Igor Korot <[email protected]> 2025-04-26 04:55 ` David G. Johnston <[email protected]> 2025-04-26 05:12 ` Igor Korot <[email protected]> 2025-04-27 04:58 ` Igor Korot <[email protected]>
This inbox is served by agora; see mirroring instructions for how to clone and mirror all data and code used for this inbox