in reply to Perl XS: garbage-collecting my malloc'd buffer
What you should do instead is switch from CODE to PPCODE and XPUSH() both the status and the return SV (which you built almost correctly--calling sv_2mortal on a value in the input list is dicey, but works correctly for return values) so that your function returns two values rather than one.
In general, you're on the right track--if you need to return a string, return an SV. (I'd go so far as to say always return an SV, but many folks would disagree)
Anyway, the code probably ought to look like:
SV * my_xs_func(p_struct, len) void *p_struct; IV len PPCODE: { char *tmp_buffer = (char *)malloc(len); XPUSHs(sv_2mortal(newSViv(my_func(p_struct, tmp_buffer, len) +))); XPUSHs(sv_2mortal(newSVpv(tmp_buffer, 0))); free(tmp_buffer); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Perl XS: garbage-collecting my malloc'd buffer (perlish?)
by tye (Sage) on Mar 03, 2003 at 17:29 UTC | |
by Elian (Parson) on Mar 03, 2003 at 17:44 UTC | |
by tye (Sage) on Mar 03, 2003 at 18:03 UTC | |
by Elian (Parson) on Mar 03, 2003 at 18:20 UTC | |
by tye (Sage) on Mar 03, 2003 at 19:09 UTC |