Re: CurrentMemoryContext and MemoryContextStrdup

Yessica Brinkmann <[email protected]> Tue, 26 Nov 2019 15:20:15 -0300
Newsgroups gmane.comp.db.postgresql.novice
Message-ID <CABrYqSPH=20JYH+f2rkEFrWBcSQC+Vj0sFChN-hEVv_NTXewRg@mail.gmail.com>
--000000000000198473059843ef0d
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

I understand. Many thanks. I will try to modify my program to adapt it to
this.
Best regards,
Yessica Brinkmann

El mar., 26 nov. 2019 a las 15:11, Tom Lane (<[email protected]>) escribi=
=C3=B3:

> Yessica Brinkmann <[email protected]> writes:
> > 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 =3D 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 =3D 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?
>
> IIRC, the real issue here is that SPI_connect creates and switches into
> a temporary context that SPI_finish will destroy; and the string you want
> is in that short-lived context.  So what you want is to copy it out of
> that context, probably into the context that was current before you
> call SPI_connect.  So what you want is to do this before calling
> SPI_connect:
>
>         MemoryContext outer_context =3D CurrentMemoryContext;
>
> and then this, inside the SPI operation, will save your string safely:
>
>         copy =3D MemoryContextStrdup(outer_context, data);
>
>                         regards, tom lane
>

--000000000000198473059843ef0d
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">I understand. Many thanks. I will try to modify my program=
 to adapt it to this.<br>Best regards,<br><div>Yessica Brinkmann</div></div=
><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">El ma=
r., 26 nov. 2019 a las 15:11, Tom Lane (&lt;<a href=3D"mailto:[email protected]=
a.us">[email protected]</a>&gt;) escribi=C3=B3:<br></div><blockquote class=
=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rg=
b(204,204,204);padding-left:1ex">Yessica Brinkmann &lt;<a href=3D"mailto:ye=
[email protected]" target=3D"_blank">[email protected]</a=
>&gt; writes:<br>
&gt; Now, as I understand it (I don&#39;t know if I&#39;m right), what I wo=
uld have to<br>
&gt; do would be this:<br>
&gt; 1. I save the CurrentMemoryContext, for example as follows:<br>
&gt; MemoryContext oldcontext =3D CurrentMemoryContext;<br>
&gt; 2. I make the call to SPI, which was what caused the context problem.<=
br>
&gt; 3. I copy my variable in a different context, for example as follows:<=
br>
&gt; MemoryContext newcontext;<br>
&gt; char * copy =3D MemoryContextStrdup (newcontext, data);<br>
&gt; 4. Then, at the end of my call to SPI, after SPI_finish () I would hav=
e in<br>
&gt; the copy variable, the copy of the data variable, to use it as I want.=
<br>
&gt; Is that so? I am correct at least to try to modify my program or is it=
<br>
&gt; totally something else what should I do?<br>
<br>
IIRC, the real issue here is that SPI_connect creates and switches into<br>
a temporary context that SPI_finish will destroy; and the string you want<b=
r>
is in that short-lived context.=C2=A0 So what you want is to copy it out of=
<br>
that context, probably into the context that was current before you<br>
call SPI_connect.=C2=A0 So what you want is to do this before calling<br>
SPI_connect:<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 MemoryContext outer_context =3D CurrentMemoryCo=
ntext;<br>
<br>
and then this, inside the SPI operation, will save your string safely:<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 copy =3D MemoryContextStrdup(outer_context, dat=
a);<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 regards, tom lane<br>
</blockquote></div>

--000000000000198473059843ef0d--