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

From: "Pfuntner, John" <John(dot)Pfuntner(at)Teradata(dot)com>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Do I have to free storage in a UDF if I raise an error?
Date: 2014-01-06 15:00:29
Message-ID: F50C41C1AF491549BDAC07CBE3CAA4B01378B8CD@SUSHDC8002.TD.TERADATA.COM
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

If I've done a palloc() to get storage inside a user-defined function and raise an error using ereport(), should I be using 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?

Responses

Browse pgsql-general by date

  From Date Subject
Next Message zach cruise 2014-01-06 15:03:10 Re: file system level backup
Previous Message Erik Darling 2014-01-06 12:55:55 Re: Dynamic SQL - transition from ms to pg