| From: | dennis jenkins <dennis(dot)jenkins(dot)75(at)gmail(dot)com> |
|---|---|
| To: | pgsql-general(at)postgresql(dot)org |
| Subject: | Re: Weird problems with C extension and bytea as input type |
| Date: | 2011-03-23 14:04:18 |
| Message-ID: | AANLkTinjNdyDN=TtBXFZscFMNhaDmHWiW3ML+YQubnpd@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-general |
On Wed, Mar 23, 2011 at 5:08 AM, Adrian Schreyer <ams214(at)cam(dot)ac(dot)uk> wrote:
>
> you are right, it returns a char *.
>
> The prototype:
>
> char *function(bytea *b);
>
> The actual C++ function looks roughly like this
>
> extern "C"
> char *function(bytea *b)
> {
> string ism;
> [...]
> return ism.c_str();
> }
>
Don't do that. You are returning a pointer to an unallocated buffer
(previously held by a local variable). c_str() is just a const
pointer to a buffer held inside "ism". When ism goes out of scope,
that buffer if freed.
Either return "std::string", or strdup() the string and have the
caller free that. (but use the postgresql alloc pool function to
handle the strdup. I don't recall that function's name off the top of
my head).
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Merlin Moncure | 2011-03-23 14:05:10 | Re: PostgreSQL documentation specifies 2-element array for float8_accum but 3-element array expected |
| Previous Message | David Johnston | 2011-03-23 13:58:54 | Re: General question |