Re: when to use pfree?

From: Ron Peterson <ron(dot)peterson(at)yellowbank(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: when to use pfree?
Date: 2006-11-07 13:55:31
Message-ID: 20061107135531.GB9684@yellowbank.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Nov 07, 2006 at 08:36:45AM -0500, Ron Peterson wrote:

> I just encountered a problem with a C function I've been working on
> where it broke with the error:
>
> could not find block containing chunk ...
>
> I tracked the problem down to my use of pfree.

I narrowed the problem down a little bit more. It has nothing to do
with the value I'm returning, it's only the call to 'pfree( rd )' below
that causes me problems.

I did the following, which apparently causes problems. I wrote my own
little function which allocates rd (using palloc).

char*
tp2cp_palloc( char* stringp, const text* textp ) {
int len;
len = VARSIZE(textp) - VARHDRSZ;
stringp = (char*)palloc( len + 1 );
if( ! memcpy( stringp, VARDATA(textp), len ) ) { return NULL; }
if( ! memset( stringp + len, '\0', 1 ) ) { return NULL; }
return stringp;
}

Which I call like

otherfunc() {
char* rd;
if( ! tp2cp_palloc( rd, rand_dev ) )
...
pfree( rd );
}

Apparently pfree hates that.

Should I abandom this idiom altogether? Or is it o.k. to do this if I
avoid the call to pfree (i.e. - will the variable be deallocated
automatically)?

TIA.

--
Ron Peterson
https://www.yellowbank.com/

> aim = TupleDescGetAttInMetadata( td );
> ht = BuildTupleFromCStrings( aim, vals);
>
> /* make the tuple into a datum */
> result = HeapTupleGetDatum( ht );
>
> ...
>
> // pfree( rd );
> // pfree( vals[0] );
> // pfree( vals[1] );
> // pfree( vals[2] );
> // pfree( vals );
>
> PG_RETURN_DATUM( result );

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2006-11-07 14:53:27 Re: when to use pfree?
Previous Message Ron Peterson 2006-11-07 13:36:45 when to use pfree?