I want to be able to safely concatenate two scalars passed into a Inline::C sub from Perl. Sounds simple, and it probably should be, but it has taken me a while to arrive at this point without generating the (extremely vague and annoying) Use of uninitialized value in subroutine entry ... warning, or getting traps when one or the other of the 2 scalars is variously undefined, or an NV, IV or UV, or read-only.
What else should I be catering for? (Preferably with hints on how to detect and handle it :).
Why, given that SV *a is created in the Perl code and is not going out of scope, do I have to increment the ref count when I return it to Perl in order to avoid Attempt to free unreferenced scalar: SV 0x182445c, Perl interpreter: 0x2240d4.?
#! perl -slw use strict; use Inline C => << '__C__', NAME => 'test', CLEAN_AFTER_BUILD => 0; #include <stdio.h> SV* test( SV *a, SV *b ) { if( SvREADONLY( a ) ) // Readonly? Take a copy. a = newSVpv( SvPVX( a ), 0 ); if( !SvPOK( a ) && ( SvNOK( a ) || SvIOK( a ) || SvUOK( a ) ) ) // + numeric SvPV_nolen( a ); // Force a string rep if( !SvPOK( a ) ) // Still nothing, must be undef? sv_setpv( a, "" ); // Make it the null string to stop (one pos +sible) // Use of uninitialized value in subroutine entry from sv_cats +v sv_catsv( a, b ); return SvREFCNT_inc( a ); // Why do I need to increment the ref c +ount? } __C__ print test( 'bill', 'fred' ); my( $p, $q ) = ( 'fred' ); print test( $q, $p ); $q = 'bill'; print test( $q, $p ); $q = 1; print test( $q, $p ); $p = 1; print test( $q, $p ); __END__ c:\test>test billfred fred billfred 1fred 1fred1
Update: This is a cleaned up version of he above function that uses the knowledge learned below to correct for a memory leak that could occure when a readonly input was supplied as argument 1.
Update2: Further simplified thanks to creamygoodness.
SV* test( SV *a, SV *b ) { if( SvREADONLY( a ) ) a = newSVpv( SvPVX( a ), 0 ); else SvREFCNT_inc( a ); if( !SvOK( a ) ) sv_setpvn( a, "", 0 ); sv_catsv( a, b ); return a; }
In reply to XS/Inline::C concat *any* two SVs. by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |