Re: Do I have to free storage in a UDF if I raise an error?

Поиск
Список
Период
Сортировка
От Stephen Woodbridge
Тема Re: Do I have to free storage in a UDF if I raise an error?
Дата
Msg-id 52CACFBA.8030509@swoodbridge.com
обсуждение исходный текст
Ответ на Do I have to free storage in a UDF if I raise an error?  ("Pfuntner, John" <John.Pfuntner@Teradata.com>)
Ответы Re: Do I have to free storage in a UDF if I raise an error?
Список pgsql-general
On 1/6/2014 10:00 AM, Pfuntner, John wrote:
> If I've done a palloc() to get storage inside a user-defined function and raise an error using ereport(),  should I
beusing pfree() to release the storage before the ereport()? 
>
> Consider this example in C:
>
> PG_FUNCTION_INFO_V1(Example);
> Datum
> Example(PG_FUNCTION_ARGS) {
>    VarChar* pstring=PG_GETARG_VARCHAR_P(0);
>    VarChar* ret=NULL;
>
>    int len = VARSIZE(pstring) - VARHDRSZ;
>    char *string=palloc(len+1);
>    memcpy(string, VARDATA(pstring), len);
>    string[len] = '\0';
>
>    /* ... */
>
>    if ( /* some bad condition */ ) {
>      ereport(ERROR, (errcode(ERRCODE_DATA_EXCEPTION), errmsg("some bad condition occurred!")));
>    }
>
>    /* ... */
>
>    pfree(string);
>
>    if (ret == NULL)
>      PG_RETURN_NULL();
>    else
>      PG_RETURN_VARCHAR_P(ret);
> }
>
> I only have the pfree() at the end before the return if there is no error.  If I fail to call pfree() before
ereport(),do I have a memory leak? 

No, this is why it is important to use palloc and malloc.

-Steve



В списке pgsql-general по дате отправления:

Предыдущее
От: David Johnston
Дата:
Сообщение: Re: Planning error in dynamic string creation in plpgsql
Следующее
От: Adrian Klaver
Дата:
Сообщение: Re: WAL archive cleanup om master