If you need a temp SV, you can create it with any of the newSV* macros (that return SVs with a reference count of 1), use it for whatever you want and finally decrement its reference count to release it:
SV *tmp = newSVsv(foo); do_something(tmp); SvREFCNT_dec(tmp);
The problem with this approach is what happens if something gets wrong inside do_something() and it calls croak. It longjmps to someplace in the perl core over your clean up code and you start leaking memory.

To solve that, you can use sv_2mortal, it remembers the SV so that, when control leaves the XS in any way, SvREFCNT_dec gets automatically called on it. The previous code is best written as:

SV *tmp = sv_2mortal(newSVsv(foo)); do_something(tmp); /* no clean up code required here */

In reply to your question, when is it necessary to call sv_2 mortal?, the answer is always, unless you want the SV to survive the XS call (for instance if you are saving a reference to it inside a C struct or global variable).

If you want to store it inside a perl container (AV or HV), the safe way to do it is:

SV *tmp = sv_2mortal(newSVsv(foo)); if (av_store(av, ix, tmp)) SvREFCNT_inc(tmp);
... av can have some magic attached (if it is a tied array for instance) and call croak from inside the av_store, that's why you need to mortalize the SV first and later increment the ref count if the call to av_store doesn't fail.

Anyway, in the API documentation (perlapi) you will find which functions change the reference count of SVs (or AVs, HVs, etc). It could be summarized as: no function touchs the ref count unless it is a constructor (which sets ref count to 1) or it is a ref count manipulation function (SvREFCNT_inc, SvREFCNT_dec, sv_2mortal). Update functions that manipulate containers (AVs, HVs or RVs) as a whole, update the refcounts of the contained elements as required, for instance, if an AV is cleared, its elements ref counts are decremented.

You can also control when mortalized variables are freed, with the SAVETMPS and FREETMPS macros. For instance, if you are creating SVs inside a loop, and you mortalize them, they are not going to be freed until the sub returns, and if it is a big loop it is going to require a lot of memory. With those macros you can free mortalized SVs created inside the loop in every iteration so the same memory is used by your temps over and over. See perlcall for more details.


In reply to Re: XS-ive Mortality. (or when to use sv_2mortal()) by salva
in thread XS-ive Mortality. (or when to use sv_2mortal()) 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.