Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Rainer Weikusat Newsgroups: comp.databases.postgresql Subject: Re: Anyone here do anything with libpq? Date: Mon, 15 May 2017 16:38:42 +0100 Lines: 30 Message-ID: <871srq6sfx.fsf@doppelsaurus.mobileactivedefense.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: individual.net qE4yNKCsl4hHI629gT2K/QatSJelcIyXzjy323+apjVLLDp3Y= Cancel-Lock: sha1:fumuttATqhlLtSsZ8FzYWB8OMVA= sha1:HUGGABmKaQY+N/zsalSNWHvF6o4= User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) Xref: csiph.com comp.databases.postgresql:772 DFS writes: > https://www.postgresql.org/docs/9.6/static/libpq.html > > > PQclear > Frees the storage associated with a PGresult. Every command result > should be freed via PQclear when it is no longer needed. > > void PQclear(PGresult *res); > > You can keep a PGresult object around for as long as you need it; it > does not go away when you issue a new command, nor even if you close > the connection. To get rid of it, you must call PQclear. Failure to do > this will result in memory leaks in your application. > > > 'when it is no longer needed' sounds like it can be cleared once, at > the end of the script. There seems to be some kind of fundamental misunderstanding about the nature of 'a result set' here. Specifically, PGresult *res; is a C pointer to a PGresult (structure) and in order to avoid memory leaks, the result this pointer points to has to be freed before the pointer is overwritten aka reused. There's no need to free the result if the pointer won't be reused, IOW, if the application is going to end next, anyway.