title: Updated: When to "mortalize" a variable in Perl Inline::C
body: <p>I am trying to wrap a C library into Perl. I have tinkered with <code>XS</code> but being unsuccessful I thought I should start simply with <code>Inline::C</code>. My question is on Mortalization. I have been reading perlguts as best as I am able, but am still confused. Do I need to call <code>sv_2mortal</code> on an SV* that is to be returned if I am not pushing it onto the stack? </p>
  
  <p>(PS I really am working on a less than functional knowledge of C which is hurting me. I have a friend who knows C helping me, but he doesn't know any Perl).</p>
  
  <p>I am providing a sample below. The function <code>FLIGetLibVersion</code> simply puts <code>len</code> characters of the library version onto char* <code>ver</code>. My question is will the <code>version_return</code> form of my C code leak memory?</p>
  
  <p>N.B. any other comments on this code is welcomed.</p>
  
  <pre><code>#!/usr/bin/perl
  
  use strict;
  use warnings;
  
  use 5.10.1;
  
  use Inline (
    C =&gt; 'DATA',
    LIBS =&gt; '-lm -lfli',
    FORCE_BUILD =&gt; 1,
  );
  
  say version_stack();
  say version_return();
  
  __DATA__
  __C__
  
  #include &lt;stdio.h&gt;
  
  #include "libfli.h"
  
  void version_stack() {
  
    Inline_Stack_Vars;
    Inline_Stack_Reset;
  
    size_t len = 50;
    char ver[len];
  
    FLIGetLibVersion(ver, len);
  
    Inline_Stack_Push(sv_2mortal(newSVpv(ver,strlen(ver))));
    Inline_Stack_Done;
  
  }
  
  SV* version_return() {
  
    size_t len = 50;
    char ver[len];
  
    FLIGetLibVersion(ver, len);
    SV* ret = newSVpv(ver, strlen(ver));
  
    return ret;
  
  }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>In an attempt to answer this myself, I tried changing the line to </p>
  
  <pre><code>SV* ret = sv_2mortal(newSVpv(ver, strlen(ver)));
  </code></pre>
  
  <p>and now when I run the script I get the same output that I did previously plus an extra warning. Here is the output:</p>
  
  <pre><code>Software Development Library for Linux 1.99
  Software Development Library for Linux 1.99
  Attempt to free unreferenced scalar: SV 0x2308aa8, Perl interpreter: 0x22cb010.
  </code></pre>
  
  <p>I imagine that this means that I don't need to mortalize in this case? I suspect that the error is saying that I marked for collection something that was already in line for collection. Can someone confirm for me that that is what that warning means?</p>