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 <markm-li...@intellasoft.net> wrote:

    On 8/4/22 02:50, demerphq wrote:
    On Thu, 4 Aug 2022 at 01:58, Mark Murawski
    <markm-li...@intellasoft.net> 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;

Reply via email to