http://qs321.pair.com?node_id=11148268

MaxPerl has asked for the wisdom of the Perl Monks concerning the following question:

Hello Seekers of Perl Wisdom,

I try to make a perl binding to the following struct

struct _Edje_Message_String_Set { int count; char *str[1]; };

At the moment my try of an implementation looks like this (only the important parts):

[...] typedef Edje_Message_String_Set EdjeMessageStringSet; MODULE = pEFL::Edje::Message::StringSet PACKAGE = pEFL::Edje::M +essage::StringSet EdjeMessageStringSet * _new(class,count, val_arr) char *class int count AV *val_arr PREINIT: EdjeMessageStringSet *message; int index; char *string; STRLEN len; CODE: message = malloc(sizeof(Edje_Message_String) + count * sizeof(char + *)); message->count = count+1; for (index = 0; index <= count; index++) { SV *tmp = *av_fetch(val_arr,index,0); string = SvPVutf8(tmp,len); message->str[index] = savepvn(string,len); } RETVAL = message; OUTPUT: RETVAL MODULE = pEFL::Edje::Message::StringSet PACKAGE = EdjeMessageSt +ringSetPtr [...] void str(message) EdjeMessageStringSet *message PREINIT: int count; char **vals; int index; PPCODE: count = message->count; vals = message->str; EXTEND(SP,count); for (index = 0; index <count; index ++) { PUSHs( sv_2mortal( newSVpv( vals[index], 0 ) )); } void DESTROY(message) EdjeMessageStringSet *message CODE: free(message);

Unfortunately I get different errors (e.g. corrupted size vs. prev_size, double_free or corruption (out), segfault, invalid pointer etc.). Following a simple test code:

use pEFL::Edje::Message::StringSet; my $i = 0; while ($i<100) { my @str = ("Hello", "Wordl", "from Perl"); my $str_msg = pEFL::Edje::Message::StringSet->new(@str); my @strings = $str_msg->str(); print "COLORS @strings\n"; $i++; } print "The script goes to the end\n";

Where is my misunderstanding?

PS:

I tried to use perl XS memory allocation, too. But this doesn't help. For example the following doesn't work:

PREINIT: [...] char **val; CODE: New(0,val,count+1, char*); New(0,message,1,EdjeMessageStringSet); message->count = count+1; for (index = 0; index <= count; index++) { SV *tmp = *av_fetch(val_arr,index,0); string = SvPVutf8(tmp,len); New(0,val[index],len,char) val[index] = savepvn(string,len); } Move(val,message->str,count+1,char*); RETVAL = message; OUTPUT: RETVAL

btw. how can one allocate memory in Perl XS with size calculated from different types (here Edje_Message_Signal_Set and char(*))?

Thank you so much for your help!!!

Max