Re: libpq error message deallocation

From: Dmitriy Igrishin <dmitigr(at)gmail(dot)com>
To: icholy <ilia(dot)choly(at)gmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: libpq error message deallocation
Date: 2012-12-04 19:14:19
Message-ID: CAAfz9KO7nASOchdZTz0MdEELy8GC407HPRHM1V5w8cGWMnefiA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

2012/12/4 icholy <ilia(dot)choly(at)gmail(dot)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.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message icholy 2012-12-04 19:45:00 Re: libpq error message deallocation
Previous Message icholy 2012-12-04 18:38:04 libpq error message deallocation