Thread: memory allocation in postgres

memory allocation in postgres

From
Kjetil Haaland
Date:
Hello all

I have written my own type and a some functions to use in postgres, but i have
some problems with the memory allocation. In one of the functions, the stuff
that is inserted is supposed to last until the user logs out, so i have
changed the memorycontext  to TopMemoryContext like this:
  MemoryContext oldcontext;
  oldcontext = MemoryContextSwitchTo(TopMemoryContext);

and back when the function is done
  MemoryContextSwitchTo(oldcontext);

The problem is, when i allocate memory in some of the other functions it is
null the first time i call it, and thats correct, but the next time i call
the same function, what was inserted in the last round is still there. Is
there away to delete it when the function is finished? I have allocated the
memory like this:
  char *res1 = NULL;
  char *res2 = NULL;
  res1 = (char *) palloc((inlength+smllength)*sizeof(char));
  res2 = (char *) palloc((inlength+smllength)*sizeof(char));

and then tried to free it in the end (i have read that you don't have to do
this but i tried it anyway, but it didn't help)
   pfree(res1);
   pfree(res2);

Hope someone can help
Regards,
Kjetil

Re: memory allocation in postgres

From
Tom Lane
Date:
Kjetil Haaland <kjetil.haaland@student.uib.no> writes:
> ... In one of the functions, the stuff
> that is inserted is supposed to last until the user logs out, so i have
> changed the memorycontext  to TopMemoryContext like this:

OK...

> The problem is, when i allocate memory in some of the other functions it is
> null the first time i call it, and thats correct, but the next time i call
> the same function, what was inserted in the last round is still there.

Isn't that what you just said you wanted?

            regards, tom lane

Re: memory allocation in postgres

From
"Kjetil Haaland"
Date:
>> The problem is, when i allocate memory in some of the other functions it
>> is
>> null the first time i call it, and thats correct, but the next time i
>> call
>> the same function, what was inserted in the last round is still there.
>
> Isn't that what you just said you wanted?
>
No and yes, in the first function i wanted it to last, but in the next i
want it to go away, so my question is how do i free the memory, so that next
time i call the function in the same session the pointer that points to the
char value, points to a null value or nothing?

-Kjetil


Re: memory allocation in postgres

From
Kjetil Haaland
Date:
On Monday 22 November 2004 22:53, Kjetil Haaland wrote:
> No and yes, in the first function i wanted it to last, but in the next i
> want it to go away, so my question is how do i free the memory, so that
> next time i call the function in the same session the pointer that points
> to the char value, points to a null value or nothing?

Hi again

Does anyone now how i do this? I have tried to use pfree(pointer) but i
doesn't seem to do anything. I print out whats in the pointer before i use
pfree and after i use it and it is the same. Am i using the wrong method or
is there any other way to do this?

thanks

-Kjetil