when to use pfree? - Mailing list pgsql-general

From Ron Peterson
Subject when to use pfree?
Date
Msg-id 20061107133645.GA9684@yellowbank.com
Whole thread Raw
Responses Re: when to use pfree?  (Ron Peterson <ron.peterson@yellowbank.com>)
List pgsql-general
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.  Apparently my function
was not happy attempting to return a result that was built using values
that had been pfree'd.  Commenting out the indicated section solved my
problem.

I understand that one of the advantages of palloc is that allocated
memory is automatically returned at some point.  My question is, when
does it make sense to call pfree?  I wouldn't have expected the
variables I free'd below to still be needed, but apparently they were.
So now I'm feeling a little intimidated about using pfree at all.
Should I just save a little wear and tear on my keyboard and forgo the
use of pfree altogether?

   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 );

TIA

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

pgsql-general by date:

Previous
From: "Woody Woodring"
Date:
Subject: Re: database name aliases?
Next
From: Ron Peterson
Date:
Subject: Re: when to use pfree?