Updated: when to "die" a variable in Perl Inline :: C

I am trying to wrap a C library in Perl. I fiddled with XS, but, being unsuccessful, I thought I should start with Inline::C. My question is mortality. I read perlguts as best as possible, but I'm still confused. Do I need to call sv_2mortalon SV *, which should be returned if I do not push it onto the stack?

(PS I'm really working on less functional C knowledge that hurts me. I have a friend who knows that C helps me, but he doesn't know any Perl.)

I provide a sample below. The function FLIGetLibVersionsimply sets the lenversion symbols of the library to char * ver. My question will be in the form of version_returnmy memory leak code C?

NB any other comments on this code are welcome.

#!/usr/bin/perl

use strict;
use warnings;

use 5.10.1;

use Inline (
  C => 'DATA',
  LIBS => '-lm -lfli',
  FORCE_BUILD => 1,
);

say version_stack();
say version_return();

__DATA__
__C__

#include <stdio.h>

#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;

}

Edit:

In an attempt to answer this myself, I tried changing the line to

SV* ret = sv_2mortal(newSVpv(ver, strlen(ver)));

and now when I run the script, I get the same result as before, plus an additional warning. Here is the result:

Software Development Library for Linux 1.99
Software Development Library for Linux 1.99
Attempt to free unreferenced scalar: SV 0x2308aa8, Perl interpreter: 0x22cb010.

I suppose this means that in this case I do not need to take a mortal risk? I suspect that the error says that I noted for the collection something that was already in the queue for collection. Can someone confirm to me that this is what this warning means?

+5
source share
1 answer

Set:: Object - , , , (github.com/samv/Set-Object), , Set:: Object . , , , SV . , Inline .

+1

All Articles