allowing multiple PQclear() calls

From: Josh Kupershmidt <schmiddy(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: allowing multiple PQclear() calls
Date: 2012-12-11 04:59:45
Message-ID: CAK3UJREYBP9rVjgra65NO36bvAJeY9SE_qnXhFCk+tzpuviE2w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

The documentation for PQclear() doesn't say whether it is safe to call
PQclear() more than once on the same PGresult pointer. In fact, it is
not safe, but apparently only because of this last step:
/* Free the PGresult structure itself */
free(res);

The other members of PGresult which may be freed by PQclear are set to
NULL or otherwise handled so as not to not be affected by a subsequent
PQclear().

I find that accounting for whether I've already PQclear'ed a given
PGresult can be quite tedious in some cases. For example, in the
cleanup code at the end of a function where control may goto in case
of a problem, it would be much simpler to unconditionally call
PQclear() without worrying about whether this was already done. One
can see an admittedly small illustration of this headache in
pqSetenvPoll() in our own codebase, where several times PQclear(res);
is called immediately before a goto error_return;

Would it be crazy to add an "already_freed" flag to the pg_result
struct which PQclear() would set, or some equivalent safety mechanism,
to avoid this hassle for users?

Josh

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jeff Davis 2012-12-11 05:33:47 Re: Commits 8de72b and 5457a1 (COPY FREEZE)
Previous Message Pavan Deolasee 2012-12-11 04:38:14 Re: Commits 8de72b and 5457a1 (COPY FREEZE)