Recently I've been working on a proprietary XS extension to speed up HTML generation from data structures in a web framework.
The XS code pretty much works and performs from 2 to 50 times better than the same implementation in Perl.

But... There's a strange test case that happens to segfault badly with an error similar to this:

Attempt to free unreferenced scalar: SV 0x01234567 at ... line xyz

Then I compiled a debugging perl. I wanted to use gdb to try to understand what's wrong. And I did. This was the result.

(gdb) b XS_MyMod_myfunc Breakpoint 2 at 0x2a97e2fdc0: file MyMod.c, line xxx. (gdb) ... Program received signal SIGSEGV, Segmentation fault. Perl_sv_unref_flags (sv=0x833a80, flags=1) at sv.c:7972 7972 if (SvREFCNT(rv) != 1 || (flags & SV_IMMEDIATE_UNREF)) (gdb) backtrace #0 Perl_sv_unref_flags (sv=0x833a80, flags=1) at sv.c:7972 #1 0x00000000004ae782 in Perl_leave_scope (base=47) at scope.c:925 #2 0x00000000004b407e in Perl_pp_return () at pp_ctl.c:1912 #3 0x000000000048659e in Perl_runops_standard () at run.c:38 #4 0x000000000043ca28 in perl_run (my_perl=Variable "my_perl" is not +available. ) at perl.c:1934 #5 0x000000000041c66d in main (argc=3, argv=0x7fbffff868, env=Variabl +e "env" is not available. ) at perlmain.c:98 (gdb)

Now, I'm definitely not a gdb expert. Any idea on how to get some more information? Now I'm going to try breakpointing and single-stepping. I suspect the problem is way before. Probably in some SV which is sv_2mortal()-ized more than necessary. When sub finally returns, the SV gets freed but it doesn't need to.

The strange thing here is that this only happens on a single x86-64 system with perl 5.8.6, while every other system I've tested the code on, i386, x86-64, with perl-5.8.6 or newer, it works perfectly...


In reply to GDB journey into perl internals by cosimo

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.