Something I'm a little unclear on. I've read Inline::C, Inline::C-Cookbook, XS-ive Mortality. (or when to use sv_2mortal()), chapter 9 from Advanced Perl Programming, and several POD's, but I guess I'm just dense. ;)

sv_2mortal() is used to decrease the ref-count on an SV "soon but not quite yet". The canonical use is when pushing values onto the stack to use as a return-value from a sub; the stack will get killed "real soon now" anyway, at the time that the Inline::C sub passes its values back to Perl.

Where I'm unclear is with this operation:

AV* return_an_array() { AV* av; av = newAV(); av_push(av,sv_2mortal(newSViv(7 ))); av_push(av,sv_2mortal(newSViv(42)));/*Re. BrowserUk: typo fixed.*/ return av; }

Here I'm pushing elements onto an anonymous array, and then passing that array by reference back to Perl (well, that's what happens after Inline::C gets its hooks into it.)

In my mind I could see this two ways, and only one of them can be right. First (the theory under which this sub performs), av_push increases the ref-count, so sv_2mortal() is needed or else the ref-count would be two at the end of the push, where we only want it to be one.

The second way is that av_push() doesn't increase the reference count. If that's the case, it would seem that sv_2mortal is putting the sub-call at risk of having all the elements in the anonymous array garbage collected upon returning to Perl, since the ref-count of each element is scheduled to drop to zero at sub-return.

In support of my first theory: It actually works. But then I wonder if I'm just living on borrowed time.

A second question: The Inline::C-Cookbook shows an example of a C subroutine calling a Perl subroutine in a way where there's no need to check the Perl sub's return value. But how about an instance where I would like to call a Perl sub from within my inline C code, and I want to see its return value? I've been unable to find an example. I did look at perlapi, and perlxstut, but it seems that the Inline::C macros interfere with the raw XS macros when I attempt to set up to check the return value.

A little guidance in the form of a few lines of code illustrating a C subroutine (ala Inline::C) calling a Perl sub and getting an int from its rv would be helpful to me.

Thanks!


Dave


In reply to When to (and not to) use sv_2mortal() by davido

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.