Ignoring the typo (newSvif), the sub as posted doesn't work -- the reference returned to Perl refers to an empty array -- and leaks memory -- the reference returned is never garbage collected.

You should (only) use sv_2mortal() on scalars that are placed on the stack. The reason is that the way the perl code gets access to these scalars is when they are copied (by assignment: my $var = someCfunc();) when the sub is called. As soon as that assignment is done, the SV placed on the stack is no longer needed or accessible, so unless you drop its refcount ("make it mortal") it will persist until the program ends. This is where your posted code is leaking, because you are not making the AV* you push onto the stack mortal. Call the function in a loop and your memory will grow and grow.

Conversely, you are making the SVs you are pushing into the (anonymous) AV mortal. As the are never copied by the call/return process -- only the ref is copied, not the referent -- by the time you try to dereference the copy of the AV* returned in order to access its contents, they have been garbage collected and the anonymous array is empty:

#! perl -slw use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => 'junk', CLEAN_AFTER_BUILD => 0; AV* return_an_array() { AV* av; av = newAV(); av_push( av, sv_2mortal( newSViv( 7 ) ) ); av_push( av, sv_2mortal( newSViv( 42 ) ) ); return av; } END_C my $ref = return_an_array(); print "@{ $ref }"; __END__ C:\test>junk Use of uninitialized value in join or string at C:\test\junk.pl line 1 +7. Use of uninitialized value in join or string at C:\test\junk.pl line 1 +7.

Run the above code in a large loop to see the memory leak.

Fixing those two errors and the program now returns the anon array of values and stops leaking:

#! perl -slw use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => 'junk', CLEAN_AFTER_BUILD => 0; AV* return_an_array() { AV* av; av = newAV(); av_push( av, newSViv( 7 ) ); av_push( av, newSViv( 42 ) ); return sv_2mortal( av ); } END_C for ( 1 .. 1e8 ) { my $ref = return_an_array(); print "@{ $ref }"; } __END__ C:\test>junk 7 42 7 42 7 42 7 42 7 42 ...
I did look at perlapi, and perlxstut,

For question 2, the documentation you need, and examples are in perlcall.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: When to (and not to) use sv_2mortal() by BrowserUk
in thread 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.