# The following was supposedly scribed by # Marvin Humphrey # on Wednesday 29 June 2005 09:30 pm:
>SV* return_a_scalar (SV* input_sv) { > if (return_the_input_unchanged) { > SvREFCNT_inc(input_sv); /* otherwise it goes to zero */ > return input_sv; > } > else { > SV* new_scalar = newSVpvn("foo", 3); > return new_scalar; > } >} > >The crux of the biscuit is that Perl doesn't know that the scalar >it's getting back is the same one it sent to C. Perl decrements the >reference count for that scalar and cleans it up. The next time you >try to access it, BAM! Invalid SV and segfault. Something sounds really wrong with that. When does a perl function ever return the same scalar that it was passed? sub return_a_scalar { my $s = shift; return($s); } Returns a *list* with the *value* of $s as the first element. I'm thinking sv_mortalcopy() (or maybe sv_setsv_flags()) is the way to go here. (I use sv_2mortal() when I'm pushing to a list, you maybe want the latter.) Particularly if you're doing something like: $var = return_a_scalar($var); Perl is going to handle the refcounting here, so I don't see why you need to mess with it. I'm pretty sure you shouldn't return(input_sv) at all, regardless of what you do with refcounts. --Eric -- "Insert random misquote here" --------------------------------------------- http://scratchcomputing.com ---------------------------------------------