Re: Having memory leak issues with perl-c

Mark Murawski <[email protected]> Fri, 12 Aug 2022 18:11:10 -0400
Newsgroups gmane.comp.lang.perl.quality-assurance
Message-ID <[email protected]>
--------------ITIys2QgDam9tEv63NQxGh6J
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit

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.




>
> 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.

Got it.

>
>>     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.
>


>
>     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()?


I'm on vacation and I'm working on this as I get the time.... I wasn't 
able to attach gdb and get anything meaningful, but based on the newly 
added debugs you can tell where it's crashing




> 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()?
>

save_item... details above..
And trimmed new code:

    // New .......... {
     FNsv = get_sv("main::_FN", GV_ADD);
     if (!FNsv) {
         ereport(ERROR,
                 (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
                  errmsg("couldn't fetch $_FN")));
     }

     fprintf(stderr, "POST get_sv\n");
     sv_dump(FNsv);

     save_item(FNsv);            /* local $_FN */

     hv = newHV();               // create new hash

     fprintf(stderr, "PRE sv_clear/sv_upgrade/SvRV_set/SvROK_on\n");

     fprintf(stderr, "PRE sv_clear\n");
     sv_clear(FNsv); /* undef the sv */
     fprintf(stderr, "POST sv_clear\n");
     sv_dump(FNsv);

     fprintf(stderr, "PRE sv_upgrade svtype: %d, %d %d\n", SvTYPE(FNsv), 
SVt_NULL, SVt_PVIO);
     sv_upgrade(FNsv,SVt_RV);
     //SvUPGRADE(FNsv, SVt_RV);
     fprintf(stderr, "POST sv_upgrade\n");
     sv_dump(FNsv);

     fprintf(stderr, "PRE sv_set/on\n");
     SvRV_set(FNsv, (SV*)hv);
     SvROK_on(FNsv);
     fprintf(stderr, "POST sv_set/on\n");
     sv_dump(FNsv);

     fprintf(stderr, "POST sv_clear/sb_upgrade/SvRV_set/SvROK_on\n");
     sv_dump(FNsv);

     // Anything below might die().. so we do all our setup above

     hv_store_string(hv, "name", cstr2sv(desc->proname));
     // new .............. }

Output:
POST get_sv
SV = NULL(0x0) at 0x55adeea14e40
   REFCNT = 1
   FLAGS = ()
PRE sv_clear/sv_upgrade/SvRV_set/SvROK_on
PRE sv_clear
POST sv_clear
SV = UNKNOWN(0xff) (0x0) at 0x55adeea14e40
   REFCNT = 1
   FLAGS = ()
PRE sv_upgrade svtype: 255, 0 15
sv_upgrade from type 255 down to type 1.
2022-08-12 17:56:57 EDT -  -  -  - 7387 -  - 0 - LOG:  server process 
(PID 11614) was terminated by signal 11: Segmentation fault


Basically my gdb session is useless and results in this:

Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007fcf6ae24c7f in ?? ()
(gdb) bt
#0  0x00007fcf6ae24c7f in ?? ()
Backtrace stopped: Cannot access memory at address 0x7ffce563e4b0



Thanks!
--------------ITIys2QgDam9tEv63NQxGh6J
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/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>
    <br>
    <br>
    <br>
    <blockquote type="cite"
cite="mid:CANgJU+W95nxb9HB_EEXUo3KDYqx-1xe14TZmJG_v-RBD=evZpw@mail.gmail.com">
      <div dir="ltr">
        <div class="gmail_quote"><br>
          <div>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.</div>
        </div>
      </div>
    </blockquote>
    <br>
    Got it.<br>
    <br>
    <blockquote type="cite"
cite="mid:CANgJU+W95nxb9HB_EEXUo3KDYqx-1xe14TZmJG_v-RBD=evZpw@mail.gmail.com">
      <div dir="ltr">
        <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> <br>
              <blockquote type="cite">
                <div dir="ltr">
                  <div class="gmail_quote">
                    <div>Obviously in perl we can write:</div>
                    <div><br>
                    </div>
                    <div>my %hash;</div>
                    <div>$main::_FN= \%hash;</div>
                    <div><br>
                    </div>
                    <div>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. </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 moz-do-not-send="true">Data::Dumper</a>
                      more or less.</div>
                  </div>
                </div>
              </blockquote>
              <br>
            </div>
          </blockquote>
        </div>
      </div>
    </blockquote>
    <br>
    <br>
    <blockquote type="cite"
cite="mid:CANgJU+W95nxb9HB_EEXUo3KDYqx-1xe14TZmJG_v-RBD=evZpw@mail.gmail.com">
      <div dir="ltr">
        <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> <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 immediately before you call
            sv_clear()? <br>
          </div>
        </div>
      </div>
    </blockquote>
    <br>
    <br>
    I'm on vacation and I'm working on this as I get the time.... I
    wasn't able to attach gdb and get anything meaningful, but based on
    the newly added debugs you can tell where it's crashing<br>
    <br>
    <br>
    <br>
    <br>
    <blockquote type="cite"
cite="mid:CANgJU+W95nxb9HB_EEXUo3KDYqx-1xe14TZmJG_v-RBD=evZpw@mail.gmail.com">
      <div dir="ltr">
        <div class="gmail_quote">
          <div>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()?</div>
          <br>
        </div>
      </div>
    </blockquote>
    <br>
    save_item... details above..<br>
    And trimmed new code:<br>
    <br>
       // New .......... {<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>
    <br>
        fprintf(stderr, "POST get_sv\n");<br>
        sv_dump(FNsv);<br>
    <br>
        save_item(FNsv);            /* local $_FN */<br>
    <br>
        hv = newHV();               // create new hash<br>
    <br>
        fprintf(stderr, "PRE sv_clear/sv_upgrade/SvRV_set/SvROK_on\n");<br>
    <br>
        fprintf(stderr, "PRE sv_clear\n");<br>
        sv_clear(FNsv); /* undef the sv */<br>
        fprintf(stderr, "POST sv_clear\n");<br>
        sv_dump(FNsv);<br>
    <br>
        fprintf(stderr, "PRE sv_upgrade svtype: %d, %d %d\n",
    SvTYPE(FNsv), SVt_NULL, SVt_PVIO);<br>
        sv_upgrade(FNsv,SVt_RV);<br>
        //SvUPGRADE(FNsv, SVt_RV);<br>
        fprintf(stderr, "POST sv_upgrade\n");<br>
        sv_dump(FNsv);<br>
    <br>
        fprintf(stderr, "PRE sv_set/on\n");<br>
        SvRV_set(FNsv, (SV*)hv);<br>
        SvROK_on(FNsv);<br>
        fprintf(stderr, "POST sv_set/on\n");<br>
        sv_dump(FNsv);<br>
    <br>
        fprintf(stderr, "POST sv_clear/sb_upgrade/SvRV_set/SvROK_on\n");<br>
        sv_dump(FNsv);<br>
    <br>
        // Anything below might die().. so we do all our setup above<br>
    <br>
        hv_store_string(hv, "name", cstr2sv(desc-&gt;proname));<br>
        // new .............. }<br>
    <br>
    Output:<br>
    POST get_sv<br>
    SV = NULL(0x0) at 0x55adeea14e40<br>
      REFCNT = 1<br>
      FLAGS = ()<br>
    PRE sv_clear/sv_upgrade/SvRV_set/SvROK_on<br>
    PRE sv_clear<br>
    POST sv_clear<br>
    SV = UNKNOWN(0xff) (0x0) at 0x55adeea14e40<br>
      REFCNT = 1<br>
      FLAGS = ()<br>
    PRE sv_upgrade svtype: 255, 0 15<br>
    sv_upgrade from type 255 down to type 1.<br>
    2022-08-12 17:56:57 EDT -  -  -  - 7387 -  - 0 - LOG:  server
    process (PID 11614) was terminated by signal 11: Segmentation fault<br>
    <br>
    <br>
    Basically my gdb session is useless and results in this:<br>
    <br>
    Program terminated with signal SIGSEGV, Segmentation fault.<br>
    #0  0x00007fcf6ae24c7f in ?? ()<br>
    (gdb) bt<br>
    #0  0x00007fcf6ae24c7f in ?? ()<br>
    Backtrace stopped: Cannot access memory at address 0x7ffce563e4b0<br>
    <br>
    <br>
    <br>
    Thanks!<br>
  </body>
</html>

--------------ITIys2QgDam9tEv63NQxGh6J--