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 (<<a href=3D"mailto:[email protected]= a.us">[email protected]</a>>) 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 <<a href=3D"mailto:ye= [email protected]" target=3D"_blank">[email protected]</a= >> writes:<br> > Now, as I understand it (I don't know if I'm right), what I wo= uld have to<br> > do would be this:<br> > 1. I save the CurrentMemoryContext, for example as follows:<br> > MemoryContext oldcontext =3D CurrentMemoryContext;<br> > 2. I make the call to SPI, which was what caused the context problem.<= br> > 3. I copy my variable in a different context, for example as follows:<= br> > MemoryContext newcontext;<br> > char * copy =3D MemoryContextStrdup (newcontext, data);<br> > 4. Then, at the end of my call to SPI, after SPI_finish () I would hav= e in<br> > the copy variable, the copy of the data variable, to use it as I want.= <br> > Is that so? I am correct at least to try to modify my program or is it= <br> > 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--