spx2 has asked for the wisdom of the Perl Monks concerning the following question:
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 :Ok so this is my XS code( SJT.xs):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; }
THE FOLLOWING LINE SEGFAULTSpackage 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); };
This is the OUTPUT I getIV 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:
the code I used to get this result was: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
Why am I getting the segfault, am I using the API wrong ?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"; };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: simple XS
by chromatic (Archbishop) on Mar 01, 2010 at 07:29 UTC | |
by ikegami (Patriarch) on Mar 01, 2010 at 12:37 UTC | |
by chromatic (Archbishop) on Mar 01, 2010 at 18:14 UTC | |
|
Re: simple XS
by almut (Canon) on Mar 01, 2010 at 06:46 UTC | |
by spx2 (Deacon) on Mar 01, 2010 at 06:49 UTC | |
by almut (Canon) on Mar 01, 2010 at 08:01 UTC | |
by ikegami (Patriarch) on Mar 01, 2010 at 12:34 UTC | |
by spx2 (Deacon) on Mar 01, 2010 at 16:00 UTC | |
|
Re: simple XS
by kthakore (Acolyte) on Mar 01, 2010 at 12:01 UTC | |
by spx2 (Deacon) on Mar 01, 2010 at 15:59 UTC |