When loading the respective core file into gdb, it indicated that the segfault had occurred when calling SvLEN_set():

$ gdb /usr/local/bin/perl core ... Core was generated by `/usr/local/bin/perl -lw ./959669.pl 128'. Program terminated with signal 11, Segmentation fault. #0 0x00007f8382eb85c5 in new (classname=0x11b1170 "fred", bits=<optim +ized out>) at test.xs:23 23 SvLEN_set( obj, bytes ); (gdb) q

My guess would be this is because the SV created with nevSV(0) doesn't yet have a LEN field.  Using newSV_type(SVt_PV) instead fixed the issue (AFAICT), i.e.

... obj = newSV_type(SVt_PV); SvPOK_only( obj ); SvPV_set( obj, vector ); SvLEN_set( obj, bytes ); SvCUR_set( obj, bytes ); ... __END__ $ ./959669.pl 128 ... SV = PVMG(0x1068f90) at 0xfb8d80 REFCNT = 1 FLAGS = (OBJECT,POK,pPOK) IV = 0 NV = 0 PV = 0x149cfa0 "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" CUR = 16 LEN = 16 STASH = 0xfd4f08 "fred"

In reply to Re: [I::C] Why does it segfault? by Eliya
in thread [I::C] Why does it segfault? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.