Re: Having memory leak issues with perl-c

[email protected] (Mark Murawski) Fri, 12 Aug 2022 19:17:14 -0400
Newsgroups perl.qa
Message-ID <[email protected]>
--------------H2Q5PUxY2WXV6PP15803El3y
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit

On 8/12/22 18:26, Mark Murawski wrote:
> On 8/12/22 18:11, Mark Murawski wrote:
>> On 8/4/22 15:10, demerphq wrote:
>>> 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 ^^
>>
>>
>> The reason for the save_item*( was because I was modeling this code 
>> on another section that uses save_item() to accomplish something 
>> similar (to send internal postgres details to some perl).  I don't 
>> know why the original author uses save_item() for this purpose.
>>
>
> Oh... I know why save_item is used.  It's because this code can be 
> executed multiple times in the same perl process.  So each one needs 
> it's own _FN
>
>



After a serious amount of trial and error... I got it to stop crashing 
and I fixed the leak


     HV         *hv;          // hash
     SV         *FNsv;        // scalar reference to the hash
     SV         *SVhv;

     ENTER;
     SAVETMPS;

     FNsv = get_sv("main::_FN", GV_ADD);
     save_item(FNsv);            /* local $_FN */

     hv = newHV();               // create new hash
     SVhv = newRV_noinc((SV *) hv);

     sv_setsv(FNsv, SVhv);
     hv_store_string(hv, "name", cstr2sv(desc->proname));

     PUTBACK;
     FREETMPS;
     LEAVE;

     SvREFCNT_dec_current(SVhv);


The fix I found was to put SvREFCNT_dec_current *after* 
PUTBACK/FREETMPS/LEAVE

if you do the SvREFCNT_dec_current prior.. then it leaks


If the trailing end of the code looks like this... you get a leak


     SvREFCNT_dec_current(SVhv);

     PUTBACK;
     FREETMPS;
     LEAVE;


--------------H2Q5PUxY2WXV6PP15803El3y
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <div class="moz-cite-prefix">On 8/12/22 18:26, Mark Murawski wrote:<br>
    </div>
    <blockquote type="cite"
      cite="mid:[email protected]">
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <div class="moz-cite-prefix">On 8/12/22 18:11, Mark Murawski
        wrote:<br>
      </div>
      <blockquote type="cite"
        cite="mid:[email protected]">
        <meta http-equiv="Content-Type" content="text/html;
          charset=UTF-8">
        <div class="moz-cite-prefix">On 8/4/22 15:10, demerphq wrote:<br>
        </div>
        <blockquote type="cite"
cite="mid:CANgJU+W95nxb9HB_EEXUo3KDYqx-1xe14TZmJG_v-RBD=evZpw@mail.gmail.com">
          <meta http-equiv="content-type" content="text/html;
            charset=UTF-8">
          <div dir="ltr">
            <div dir="ltr">On Thu, 4 Aug 2022 at 17:04, Mark Murawski
              &lt;<a href="mailto:[email protected]"
                moz-do-not-send="true" class="moz-txt-link-freetext">[email protected]</a>&gt;
              wrote:<br>
            </div>
            <div class="gmail_quote">
              <blockquote class="gmail_quote" style="margin:0px 0px 0px
                0.8ex;border-left:1px solid
                rgb(204,204,204);padding-left:1ex">
                <div>
                  <div>On 8/4/22 02:50, demerphq wrote:<br>
                  </div>
                  <blockquote type="cite">
                    <div dir="ltr">
                      <div dir="ltr">On Thu, 4 Aug 2022 at 01:58, Mark
                        Murawski &lt;<a
                          href="mailto:[email protected]"
                          target="_blank" moz-do-not-send="true"
                          class="moz-txt-link-freetext">[email protected]</a>&gt;
                        wrote:<br>
                      </div>
                      <div class="gmail_quote">
                        <blockquote class="gmail_quote"
                          style="margin:0px 0px 0px
                          0.8ex;border-left:1px solid
                          rgb(204,204,204);padding-left:1ex">
                          <div>
                            <div>I'm still not getting something... if I
                              want to fix the code-as-is and do this:<br>
                            </div>
                            <br>
                                FNsv = get_sv("main::_FN", GV_ADD);<br>
                                if (!FNsv)<br>
                                    ereport(ERROR,<br>
                                           
                            (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),<br>
                                             errmsg("couldn't fetch
                            $_FN")));<br>
                            <br>
                                save_item(FNsv);            /* 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>
          </div>
        </blockquote>
        <br>
        <br>
        The reason for the save_item*( was because I was modeling this
        code on another section that uses save_item() to accomplish
        something similar (to send internal postgres details to some
        perl).  I don't know why the original author uses save_item()
        for this purpose.<br>
        <br>
      </blockquote>
      <br>
      Oh... I know why save_item is used.  It's because this code can be
      executed multiple times in the same perl process.  So each one
      needs it's own _FN<br>
      <br>
      <br>
    </blockquote>
    <br>
    <br>
    <br>
    After a serious amount of trial and error... I got it to stop
    crashing and I fixed the leak<br>
    <br>
    <br>
        HV         *hv;          // hash<br>
        SV         *FNsv;        // scalar reference to the hash<br>
        SV         *SVhv;<br>
    <br>
        ENTER;<br>
        SAVETMPS;<br>
    <br>
        FNsv = get_sv("main::_FN", GV_ADD);<br>
        save_item(FNsv);            /* local $_FN */<br>
    <br>
        hv = newHV();               // create new hash<br>
        SVhv = newRV_noinc((SV *) hv);<br>
    <br>
        sv_setsv(FNsv, SVhv);<br>
        hv_store_string(hv, "name", cstr2sv(desc-&gt;proname));<br>
    <br>
        PUTBACK;<br>
        FREETMPS;<br>
        LEAVE;<br>
    <br>
        SvREFCNT_dec_current(SVhv);<br>
    <br>
    <br>
    The fix I found was to put SvREFCNT_dec_current *after*
    PUTBACK/FREETMPS/LEAVE<br>
    <br>
    if you do the SvREFCNT_dec_current prior.. then it leaks<br>
    <br>
    <br>
    If the trailing end of the code looks like this... you get a leak<br>
    <br>
    <br>
        SvREFCNT_dec_current(SVhv);<br>
    <br>
        PUTBACK;<br>
        FREETMPS;<br>
        LEAVE;<br>
    <br>
    <br>
  </body>
</html>

--------------H2Q5PUxY2WXV6PP15803El3y--