libpq error message deallocation

Started by icholyover 13 years ago3 messagesgeneral
Jump to latest
#1icholy
ilia.choly@gmail.com

PQerrorMessage function return char const*
char const* msg = PQerrorMessage(conn);
Now since it's const, I don't think I should be deallocating it and I've
never seen that done in any examples. But then, when and how does it get
freed?
At first I thought it gets deallocated once another error message is
requested but that's not the case.
// cause some error char const* msg1 = PQerrorMessage(pgconn); //
cause another error char const* msg2 = PQerrorMessage(pgconn); //
still works std::cout << msg1 << msg2 << std::endl;
Can someone shed some light on this for me?

--
View this message in context: http://postgresql.1045698.n5.nabble.com/libpq-error-message-deallocation-tp5735032.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

#2Dmitriy Igrishin
dmitigr@gmail.com
In reply to: icholy (#1)
Re: libpq error message deallocation

2012/12/4 icholy <ilia.choly@gmail.com>

PQerrorMessage function return char const*

char const* msg = PQerrorMessage(conn);

Now since it's const, I don't think I should be deallocating it and I've
never seen that done in any examples. But then, when and how does it get
freed?

At first I thought it gets deallocated once another error message is
requested but that's not the case.

// cause some error
char const* msg1 = PQerrorMessage(pgconn);

// cause another error
char const* msg2 = PQerrorMessage(pgconn);

// still works
std::cout << msg1 << msg2 << std::endl;

Can someone shed some light on this for me?

PQerrorMessage() returns pointer to the last allocated string
from the PGConn. The memory on this string will be deallocated
with PQfinish().
In the above case, msg1 is invalid pointer and you just got lucky.
Please, see description of PQerrorMessage() here
http://www.postgresql.org/docs/9.2/static/libpq-status.html

// Dmitriy.

#3icholy
ilia.choly@gmail.com
In reply to: Dmitriy Igrishin (#2)
Re: libpq error message deallocation

thanks for the quick reply!

--
View this message in context: http://postgresql.1045698.n5.nabble.com/libpq-error-message-deallocation-tp5735032p5735046.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general