I've been reading through perlapi , perlxs , perlxstut and this here book , however I have a problem. I'm trying to create a class, and access one of its attributes from XS code and I get segfault. I tried to do a very simple thing, to write XS that would access an certain position of an arrayref and return it. Maybe I'm using the wrong API ?

this is my class SJT.pm :
package SJT; use 5.010000; use strict; use warnings; require Exporter; our @ISA = qw(Exporter); our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw( ); our $VERSION = '0.01'; require XSLoader; XSLoader::load('SJT', $VERSION); sub new { my ($class) = @_; return bless { permutation => [2,2,2,2,2,2,2,2,2,2,2], },$class; }
Ok so this is my XS code( SJT.xs):
package SJT; use 5.010000; use strict; use warnings; require Exporter; our @ISA = qw(Exporter); MODULE = SJT PACKAGE = SJT //THIS FUNCTION SHOULD JUST GET $self->permutation->[index] int get(self,index) SV* self int index CODE: char* key = "permutation"; AV* array; SV* hv = self; if(sv_isobject(hv)) { printf("self is object,moving on...\n"); } else { printf("SJT::get was expecting self to be an object"); }; HV* q = (HV *)SvRV(hv); array = *hv_fetch(q,"permutation",11,FALSE); if(array==NULL) { printf("array not found in self...\n"); exit(-1); }else { printf("array found in self %X\n",array); }; SV** res = av_fetch(array,index,FALSE); if(res==NULL) { printf("item not found in array...\n"); exit(-1); }else { printf("also found item in array at: %X\n",res); };
THE FOLLOWING LINE SEGFAULTS
IV a = SvIV(SvRV(*res)); printf("right before return\n"); RETVAL = a;//SvIV(*av_fetch(array,index,FALSE)); OUTPUT: RETVAL void set(self,index,value) SV* self int index; int value; CODE: OUTPUT:
This is the OUTPUT I get
ok 1 - use SJT; SV = RV(0x82807dc) at 0x82807d0 REFCNT = 1 FLAGS = (PADMY,ROK) RV = 0x82bc750 SV = PVHV(0x8185174) at 0x82bc750 REFCNT = 1 FLAGS = (OBJECT,SHAREKEYS) STASH = 0x82bc9e0 "SJT" ARRAY = 0x82ccd20 (0:7, 1:1) hash quality = 100.0% KEYS = 1 FILL = 1 MAX = 7 RITER = -1 EITER = 0x0 Elt "permutation" HASH = 0x41573374 SV = RV(0x82bc6ac) at 0x82bc6a0 REFCNT = 1 FLAGS = (ROK) RV = 0x8242ce0 SV = PVAV(0x828d814) at 0x8242ce0 REFCNT = 1 FLAGS = () ARRAY = 0x82cbcb8 FILL = 10 MAX = 10 ARYLEN = 0x0 FLAGS = (REAL) Elt No. 0 SV = IV(0x82bc88c) at 0x82bc890 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 2 Elt No. 1 SV = IV(0x82bc70c) at 0x82bc710 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 2 Elt No. 2 SV = IV(0x82bc6ec) at 0x82bc6f0 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 2 Elt No. 3 SV = IV(0x82bc6fc) at 0x82bc700 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 2 self is object,moving on... array found in self 82BC6A0 also found item in array at: 8242CE4 19830 Segmentation fault
the code I used to get this result was:
use Devel::Peek; use Data::Dumper; use Test::More tests => 1; BEGIN { use_ok('SJT'); my $s1 = SJT->new; Dump($s1); print "HERE be 2:".$s1->get(1)."\n"; print "\n"; };
Why am I getting the segfault, am I using the API wrong ?

In reply to simple XS by spx2

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.