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


In reply to Perl XS binding to a struct with an array of chars* by MaxPerl

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.