From: | Yessica Brinkmann <yessica(dot)brinkmann(at)gmail(dot)com> |
---|---|
To: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
Cc: | pgsql-novice(at)lists(dot)postgresql(dot)org |
Subject: | Re: CurrentMemoryContext and MemoryContextStrdup |
Date: | 2019-11-26 17:34:32 |
Message-ID: | CABrYqSOv2GEHmZcOmJPrrhVTmO-HHActL7z54b9yxO1H61r_wA@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-novice |
Thank you very much for your answer.
Well, you told me that pstrdup was not going to help me in the context of
my use case.
And that I would have to use MemoryContextStrdup.
And the truth is that this part costs me a bit, even though I have the
definitions of MemoryContextStrdup in the .c, since I can't find an example
of a source code that uses it, it's hard for me to apply things by
definition just.
By telling me: "Copying it into a different context would be the way to do
that", I understand better.
Now, as I understand it (I don't know if I'm right), what I would have to
do would be this:
1. I save the CurrentMemoryContext, for example as follows:
MemoryContext oldcontext = CurrentMemoryContext;
2. I make the call to SPI, which was what caused the context problem.
3. I copy my variable in a different context, for example as follows:
MemoryContext newcontext;
char * copy = MemoryContextStrdup (newcontext, data);
4. Then, at the end of my call to SPI, after SPI_finish () I would have in
the copy variable, the copy of the data variable, to use it as I want.
Is that so? I am correct at least to try to modify my program or is it
totally something else what should I do?
Best regards,
Yessica Brinkmann
El mar., 26 nov. 2019 a las 13:48, Tom Lane (<tgl(at)sss(dot)pgh(dot)pa(dot)us>) escribió:
> Yessica Brinkmann <yessica(dot)brinkmann(at)gmail(dot)com> writes:
> > Thank you very much for the reply!
> > Not really, I don't feel better informed because MemoryContextStrdup is
> not
> > even mentioned once in the README.
>
> The next thing to do would be to look at that function's header comment
> (find it in src/backend/utils/mmgr/mcxt.c):
>
> /*
> * MemoryContextStrdup
> * Like strdup(), but allocate from the specified context
> */
> char *
> MemoryContextStrdup(MemoryContext context, const char *string)
>
>
> As I recall your original problem, people were suggesting that
> you make a longer-lived copy of some transiently-allocated
> string. Copying it into a different context would be the
> way to do that, as I hope you now understand from the README
> discussion, and this function is the easiest way to do that.
>
> Or at least the second easiest; the very easiest is pstrdup,
> which is just
>
> char *
> pstrdup(const char *in)
> {
> return MemoryContextStrdup(CurrentMemoryContext, in);
> }
>
> but I don't remember whether the current context was a suitable
> target for your use-case.
>
> regards, tom lane
>
From | Date | Subject | |
---|---|---|---|
Next Message | Tom Lane | 2019-11-26 18:11:00 | Re: CurrentMemoryContext and MemoryContextStrdup |
Previous Message | Tom Lane | 2019-11-26 16:48:21 | Re: CurrentMemoryContext and MemoryContextStrdup |