On Thu, Oct 30, 2003 at 11:13:47AM +0000 Nick Ing-Simmons wrote: > Sisyphus <[EMAIL PROTECTED]> writes: > >Tassilo von Parseval wrote:
> >> It is. If you have code like > >> > >> { > >> my $var = my_xsub(); > >> ... > >> } > >> > >> and want $var to be cleaned up when it falls out of scope, you have to > >> make it a mortal. Mortals are just variables that clean up themselves > >> when their reference count goes to zero. > >> > > > >I still question that - and hope that in so doing I don't raise your > >anger. (That's not what I'm trying to do :-) > > You are right. > > The sv_2mortal _IS_ needed in the > > void > my_xsub() > CODE: > { > ST(0) = sv_2mortal(newSVpv(string,strlen(string)); > XSRETURN(1); > } > > case. > > But it is inserted for you SV * return case: > > { > RETVAL=newSV...; > } > #line 25 "Foo.c" > ST(0) = RETVAL; > sv_2mortal(ST(0)); > } > XSRETURN(1); > } Hmmh, indeed. I peeked at xsubpp and it looks as though this is a special case: elsif ($expr =~ /^\s*\$arg\s*=/) { # We expect that $arg has refcnt >=1, so we need # to mortalize it! eval "print qq\a$expr\a"; warn $@ if $@; print "\tsv_2mortal(ST(0));\n"; print "\tSvSETMAGIC(ST(0));\n" if $do_setmagic; } The typemap entry for 'SV*' expands to $arg = $var However, doing char * my_xsub() CODE: RETVAL = strdup(string); OUTPUT: RETVAL will leak since the returned SV is not mortalized (and the copy of 'string' thus never freed). I think this is one good reason for sticking to one style of handling return values in XS since they differ quite significantly. I prefer the explicit manipulation of the return stack by feeding it my manually constructed SV's. That way, I can be sure that each scalar has the characteristics it is supposed to have. Tassilo -- $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({ pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#; $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval