[C-function] Can SET_VARSIZE cause a memory leak?

From: Николай Бабаджанян <nb(at)cobra(dot)ru>
To: pgsql-general(at)postgresql(dot)org
Subject: [C-function] Can SET_VARSIZE cause a memory leak?
Date: 2016-06-08 11:47:29
Message-ID: 1019310536.5581.1465386449497.JavaMail.zimbra@cobra.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I didn't find an easy way to convert ucs-2 bytea to utf-8, so I decided to write a C-function. Since ucs-2 is has fixed symbol size of 2 bytes the output bytea size may differ.

I do the following:

bytea *result= (bytea *) palloc0(VARSIZE(in_bytea)); // allocating memory for the result
SET_VARSIZE(result, VARSIZE_ANY(in_bytea));

... // some calculations resulting in `result` having some trailing 0-s (since palloc0 was used). We don't need those, so:

SET_VARSIZE(result, new_varsize_result+VARHDRSZ); // new_varsize_result was calculated during the convertion

PG_RETURN_BYTEA_P(result);

The question is am I leaking memory by doing this, and if I am, should I use pfree() manually on each address that is left trailing, or is there some other way to make this work?

Regards,
Nick.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Albe Laurenz 2016-06-08 12:55:09 Re: Can SET_VARSIZE cause a memory leak?
Previous Message Artur Zakirov 2016-06-08 10:10:37 Re: [pg_trgm] Making similarity(?, ?) < ? use an index