Re: Having memory leak issues with perl-c
[email protected] (Mark Murawski) Thu, 4 Aug 2022 11:03:57 -0400
| Newsgroups | perl.qa |
|---|---|
| Message-ID | <[email protected]> |
--------------KOF2zIMT9xk9r4RKZncQJ63D Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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. > > > 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? > 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. > 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? I still have a leak here, using the above code. Also... i get a crash when I use sv_clear(FNsv); right away like this. 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. Thanks. --------------KOF2zIMT9xk9r4RKZncQJ63D 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 02:50, demerphq wrote:<br> </div> <blockquote type="cite" cite="mid:CANgJU+U0B9-2LN0QS1w7r6CR+VdFYhSMdzidy2cq4aqSzWv6qA@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 01:58, Mark Murawski <<a href="mailto:[email protected]" moz-do-not-send="true" class="moz-txt-link-freetext">[email protected]</a>> 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 class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <div> <br> hv = newHV(); // create new hash<br> hv_store_string(hv, "name", cstr2sv(desc->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's nothing leaks.</div> </div> </div> </blockquote> <br> 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?<br> <br> <br> <blockquote type="cite" cite="mid:CANgJU+U0B9-2LN0QS1w7r6CR+VdFYhSMdzidy2cq4aqSzWv6qA@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> WARNING: 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'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> <br> <blockquote type="cite" cite="mid:CANgJU+U0B9-2LN0QS1w7r6CR+VdFYhSMdzidy2cq4aqSzWv6qA@mail.gmail.com"> <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 class="moz-txt-link-freetext" href="Data::Dumper">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? I still have a leak here, using the above code.<br> <br> Also... i get a crash when I use sv_clear(FNsv); right away like this.<br> 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.<br> <br> <br> Thanks.<br> </body> </html> --------------KOF2zIMT9xk9r4RKZncQJ63D--