Re: Having memory leak issues with perl-c

demerphq <[email protected]> Thu, 4 Aug 2022 21:10:57 +0200
Newsgroups gmane.comp.lang.perl.quality-assurance
Message-ID <CANgJU+W95nxb9HB_EEXUo3KDYqx-1xe14TZmJG_v-RBD=evZpw@mail.gmail.com>
--000000000000b5508f05e56f1c26
Content-Type: text/plain; charset="UTF-8"

On Thu, 4 Aug 2022 at 17:04, Mark Murawski <[email protected]>
wrote:

> On 8/4/22 02:50, demerphq wrote:
>
> On Thu, 4 Aug 2022 at 01:58, Mark Murawski <[email protected]>
> wrote:
>
>> I'm still not getting something... if I want to fix the code-as-is and do
>> this:
>>
>>     FNsv = get_sv("main::_FN", GV_ADD);
>>     if (!FNsv)
>>         ereport(ERROR,
>>                 (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
>>                  errmsg("couldn't fetch $_FN")));
>>
>>     save_item(FNsv);            /* local $_FN */
>>
>
> I dont get the sequence here. You take the old value of $main::_FN and
> then you localize it after you fetch it? That seems weird.
>
>
You did not respond to this comment ^^


>
>
>>
>>     hv = newHV(); // create new hash
>>     hv_store_string(hv, "name", cstr2sv(desc->proname));
>>
>
> Really you shouldnt do this until you have safely managed the refcounts of
> all your newly created objects so that if this die's nothing leaks.
>
>
> I take this to mean , setting up FNsv first, and then allocating hv?  But
> in this case we seem to have a chicken/egg problem?  How can you set up
> FNsv to point to hv without first setting up hv?
>

No, you shouldn't do anything after creating the hash that might die until
you have arranged for hv to be freed. Storing into the hash might die.


>
> WARNING:  Attempt to free unreferenced scalar: SV 0x55d5b1cf6480, Perl
>> interpreter: 0x55d5b17226c0
>>
>
> Why are you decrementing hv? You dont own hv anymore, it's owned by svFN
> and after the sv_setsv() call also FNsv. You shouldnt mess with its
> refcount anymore.
>
>
> The ownership aspect is making more sense now, thanks for clarifying.
>

YW.


>
> Obviously in perl we can write:
>
> my %hash;
> $main::_FN= \%hash;
>
> And in XS we can do the same thing. Unfortunately there isn't a utility
> sub to do this currently, it has been on my TODO list to add one for some
> time but lack of round tuits and all that.
>
> You want code something like this:
>
> sv_clear(FNsv); /* undef the sv */
> sv_upgrade(FNsv,SVt_RV);
> SvRV_set(FNsv, (SV*)hv);
> SvROK_on(FNsv);
>
> Again, make liberal use of sv_dump() it is the XS version of Data::Dumper
> more or less.
>
>
> I have been playing with sv_dump()... At the end of this flow, the
> refcount to FNsv is 1 and should get automatically cleaned up by Perl,
> right?
>

Well I dont know for sure as I dont know the latest state of your code. But
my thinking is that you asked for $main::_FN which is in the package table,
so perl should clean it up. But I dont understand why you are using
save_item(FNsv).


> I still have a leak here, using the above code.
>

Probably I would be able to help you  better if you posted the latest state
of your code, with sv_dump() calls liberally sprinkled through the code,
and post the output as well.


>
> Also... i get a crash when I use sv_clear(FNsv); right away like this.
>

And what does FNsv look like immediately before you call sv_clear()?


> If I take it out, the code seems to all run correctly, but I have a leak
> and the hash or the hash reference is not being cleaned up.
>

Can you please show a reduced version of the code? And explain why you are
doing save_item(FNsv)? And provide some of the output of sv_dump()?

cheers,
Yves

-- 
perl -Mre=debug -e "/just|another|perl|hacker/"

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

<div dir=3D"ltr"><div dir=3D"ltr">On Thu, 4 Aug 2022 at 17:04, Mark Murawsk=
i &lt;<a href=3D"mailto:[email protected]">markm-lists@intellasof=
t.net</a>&gt; wrote:<br></div><div class=3D"gmail_quote"><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">
 =20
   =20
 =20
  <div>
    <div>On 8/4/22 02:50, demerphq wrote:<br>
    </div>
    <blockquote type=3D"cite">
     =20
      <div dir=3D"ltr">
        <div dir=3D"ltr">On Thu, 4 Aug 2022 at 01:58, Mark Murawski &lt;<a =
href=3D"mailto:[email protected]" target=3D"_blank">markm-lists@i=
ntellasoft.net</a>&gt;
          wrote:<br>
        </div>
        <div class=3D"gmail_quote">
          <blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8=
ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
            <div>
              <div>I&#39;m still not getting something... if I want to fix
                the code-as-is and do this:<br>
              </div>
              <br>
              =C2=A0=C2=A0=C2=A0 FNsv =3D get_sv(&quot;main::_FN&quot;, GV_=
ADD);<br>
              =C2=A0=C2=A0=C2=A0 if (!FNsv)<br>
              =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ereport(ERROR,<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=C2=A0=C2=A0=C2=A0
              (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),<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=C2=A0=C2=A0=C2=A0=C2=A0 errmsg(&quot;couldn&#39;t fetch $_FN&q=
uot;)));<br>
              <br>
              =C2=A0=C2=A0=C2=A0 save_item(FNsv);=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 /* local $_FN */<br>
            </div>
          </blockquote>
          <div><br>
          </div>
          <div>I dont get the sequence here. You take the old value of
            $main::_FN and then you localize it after you fetch it? That
            seems weird.</div></div></div></blockquote></div></blockquote><=
div><br></div><div>You did not respond to this comment ^^</div><div>=C2=A0<=
/div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;bo=
rder-left:1px solid rgb(204,204,204);padding-left:1ex"><div><blockquote typ=
e=3D"cite"><div dir=3D"ltr"><div class=3D"gmail_quote">
          <div>=C2=A0</div>
          <blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8=
ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
            <div> <br>
              =C2=A0=C2=A0=C2=A0 hv =3D newHV(); // create new hash<br>
              =C2=A0=C2=A0=C2=A0 hv_store_string(hv, &quot;name&quot;,
              cstr2sv(desc-&gt;proname));<br>
            </div>
          </blockquote>
          <div><br>
          </div>
          <div>Really you shouldnt do this until you have safely managed
            the refcounts of all your newly created objects so that if
            this die&#39;s nothing leaks.</div>
        </div>
      </div>
    </blockquote>
    <br>
    I take this to mean , setting up FNsv first, and then allocating
    hv?=C2=A0 But in this case we seem to have a chicken/egg problem?=C2=A0=
 How
    can you set up FNsv to point to hv without first setting up hv?<br></di=
v></blockquote><div><br></div><div>No, you shouldn&#39;t do anything after =
creating the hash that might die until you have arranged for hv to be freed=
. Storing into the hash might die.</div><div><br></div><blockquote class=3D=
"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(2=
04,204,204);padding-left:1ex"><div>
    <br>
    <br>
    <blockquote type=3D"cite">
      <div dir=3D"ltr">
        <div class=3D"gmail_quote">
          <blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8=
ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
            <div> WARNING:=C2=A0 Attempt to free unreferenced scalar: SV
              0x55d5b1cf6480, Perl interpreter: 0x55d5b17226c0<br>
            </div>
          </blockquote>
          <div><br>
          </div>
          <div>Why are you decrementing hv? You dont own hv anymore,
            it&#39;s owned by svFN and after the sv_setsv() call also FNsv.
            You shouldnt mess with its refcount anymore.</div>
        </div>
      </div>
    </blockquote>
    <br>
    The ownership aspect is making more sense now, thanks for
    clarifying.<br></div></blockquote><div><br></div><div>YW.</div><div>=C2=
=A0</div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8e=
x;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>
    <br>
    <blockquote type=3D"cite">
      <div dir=3D"ltr">
        <div class=3D"gmail_quote">
          <div>Obviously in perl we can write:</div>
          <div><br>
          </div>
          <div>my %hash;</div>
          <div>$main::_FN=3D \%hash;</div>
          <div><br>
          </div>
          <div>And in XS we can do the same thing. Unfortunately there
            isn&#39;t=C2=A0a utility sub to do this currently, it has=C2=A0=
been on my
            TODO list to add one for some time but lack of round=C2=A0tuits
            and all that.=C2=A0</div>
          <div><br>
          </div>
          <div>You want code something like this:</div>
          <div><br>
          </div>
          <div>sv_clear(FNsv); /* undef the sv */</div>
          <div>sv_upgrade(FNsv,SVt_RV);</div>
          <div>SvRV_set(FNsv, (SV*)hv);</div>
          <div>SvROK_on(FNsv);</div>
          <div><br>
          </div>
          <div>Again, make liberal use of sv_dump() it is the XS version
            of <a>Data::Dumper</a> more or less.</div>
        </div>
      </div>
    </blockquote>
    <br>
    I have been playing with sv_dump()... At the end of this flow, the
    refcount to FNsv is 1 and should get automatically cleaned up by
    Perl, right?=C2=A0 </div></blockquote><div><br></div><div>Well I dont k=
now for sure as I dont know the latest state of your code. But my thinking =
is that you asked for $main::_FN which is in the package table, so perl sho=
uld clean it up. But I dont understand why you are=C2=A0using save_item(FNs=
v).</div><div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin=
:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"=
><div>I still have a leak here, using the above code.<br></div></blockquote=
><div><br></div><div>Probably I would be able to help you=C2=A0 better if y=
ou posted the latest state of your code, with sv_dump() calls liberally spr=
inkled through the code, and post the output as well.</div><div>=C2=A0</div=
><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border=
-left:1px solid rgb(204,204,204);padding-left:1ex"><div>
    <br>
    Also... i get a crash when I use sv_clear(FNsv); right away like
    this.<br></div></blockquote><div><br>And what does FNsv look like immed=
iately before you call sv_clear()?=C2=A0</div><div>=C2=A0</div><blockquote =
class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px sol=
id rgb(204,204,204);padding-left:1ex"><div>
    If I take it out, the code seems to all run correctly, but I have a
    leak and the hash or the hash reference is not being cleaned up.</div><=
/blockquote><div><br></div><div>Can you please show a reduced version of th=
e code? And explain why you are doing save_item(FNsv)? And provide some of =
the output of sv_dump()?</div><div><br></div><div>cheers,</div><div>Yves</d=
iv><div><br></div></div>-- <br><div dir=3D"ltr" class=3D"gmail_signature">p=
erl -Mre=3Ddebug -e &quot;/just|another|perl|hacker/&quot;</div></div>

--000000000000b5508f05e56f1c26--