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.





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!

Reply via email to