Search Postgresql Archives

Re: when to use pfree?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux