allowing multiple PQclear() calls - Mailing list pgsql-hackers

From Josh Kupershmidt
Subject allowing multiple PQclear() calls
Date
Msg-id CAK3UJREYBP9rVjgra65NO36bvAJeY9SE_qnXhFCk+tzpuviE2w@mail.gmail.com
Whole thread Raw
Responses Re: allowing multiple PQclear() calls  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: allowing multiple PQclear() calls  (Marko Kreen <markokr@gmail.com>)
List 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



pgsql-hackers by date:

Previous
From: Pavan Deolasee
Date:
Subject: Re: Commits 8de72b and 5457a1 (COPY FREEZE)
Next
From: Jeff Davis
Date:
Subject: Re: Commits 8de72b and 5457a1 (COPY FREEZE)