Should I call SvREFCNT_dec () on an SV that will not be returned to Perl on the stack?

When calling a C function from Perl, for example, using Inline::C:

use feature qw(say);
use strict;
use warnings;
use Inline C => './test.c';

say "Calling test()..";
test();
say "Finished.";

where test.c:

void test() 
{
    SV *sv_variable = newSVpv("test", 0);

    // do something..

    //SvREFCNT_dec(sv_variable); // free variable
    printf( "Returning from test()..\n");
    return;

}

The script seems to be working fine, I am calling SvREFCNT_dec(sv_variable)or not. According to perlguts:

To free the created SV, call SvREFCNT_dec (SV *). As usual this call is not needed

+4
source share
1 answer

Yes, you must reduce the conversion factor. (If you do not, there are no immediate bad effects, but you created a memory leak.)

perlguts, , , , SV C; , Perl .

, : - // do something throws, sv_variable ( SvREFCNT_dec ). , :

SV *sv_variable = sv_2mortal(newSVpv("test", 0));

sv_2mortal SvREFCNT_dec: "".

( SV , newSVpvs("test") , newSVpv, .)

+4

Source: https://habr.com/ru/post/1670823/


All Articles