Re: Weird problems with C extension and bytea as input type
От | dennis jenkins |
---|---|
Тема | Re: Weird problems with C extension and bytea as input type |
Дата | |
Msg-id | AANLkTinjNdyDN=TtBXFZscFMNhaDmHWiW3ML+YQubnpd@mail.gmail.com обсуждение исходный текст |
Ответ на | Re: Weird problems with C extension and bytea as input type (Adrian Schreyer <ams214@cam.ac.uk>) |
Ответы |
Re: Weird problems with C extension and bytea as input type
|
Список | pgsql-general |
On Wed, Mar 23, 2011 at 5:08 AM, Adrian Schreyer <ams214@cam.ac.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).
В списке pgsql-general по дате отправления: