Re: when to use pfree?

Поиск
Список
Период
Сортировка
От Ron Peterson
Тема Re: when to use pfree?
Дата
Msg-id 20061107135531.GB9684@yellowbank.com
обсуждение исходный текст
Ответ на when to use pfree?  (Ron Peterson <ron.peterson@yellowbank.com>)
Ответы Re: when to use pfree?  (Tom Lane <tgl@sss.pgh.pa.us>)
Список 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 );



В списке pgsql-general по дате отправления:

Предыдущее
От: Ron Peterson
Дата:
Сообщение: when to use pfree?
Следующее
От: Tom Lane
Дата:
Сообщение: Re: when to use pfree?