Steve Hay <[EMAIL PROTECTED]> writes: >Why is it that when I create an SV and assign magic to it in an XSUB, >and then return the magical SV to Perl code, I find that the scalar in >the Perl code no longer seems to have magic?
If you print something the FooFree() you will see what is happening. You return an SV. SV gets copied to $a - magic isn't copied Your SV gets free-d $a is passed to your function. Normally this doesn't matter as "objects" return references to the thing which has magic. > >The attached sample module has XSUB's to create a magical SV (fooalloc >and fooallocext), an XSUB to use the magical SV created (foo) and >another to free it (foofree, not strictly necessary since the SV here is >mortal anyway). > >I was intending that when I run > >perl -Mblib -MFoo -e "$a=Foo::fooalloc();Foo::foo($a);Foo::foofree($a)" > >it would print "Hello, world.". > >But it doesn't. It croak()'s in foo() claiming that it can't find the >magic on the SV passed in. Because $a hasn't got any. You could make it work if you passed in the SV: Foo::fooalloc($a) And I think Foo::foo(Foo::fooalloc()) will work. > >I've added some sv_dump()'s into the allocators to look at the SV being >returned; using Devel::Peek::Dump() in the Perl code I can also look at >what it received, and I find that they're completely different: > >perl -Mblib -MFoo -MDevel::Peek -e "$a=Foo::fooalloc();Dump($a)" > >outputs this: > >SV = PVMG(0x1835170) at 0x1820184 ^^^^^^^^^ - Your SV > REFCNT = 1 > FLAGS = (TEMP,RMG) > IV = 0 > NV = 0 > PV = 0 > MAGIC = 0x182a188 > MG_VIRTUAL = 0x354410 > MG_TYPE = PERL_MAGIC_ext(~) > MG_PTR = 0x185a738 "" The dump of the temporary (mortal) you return. >SV = PVMG(0x1835190) at 0x183578c ^^^^^^^^^ - SV for $a > REFCNT = 1 > FLAGS = () > IV = 0 > NV = 0 > PV = 0 The dump of SV for $a. > >void >foofree(sv) > SV *sv; >PPCODE: >{ > SvREFCNT_dec(sv); >} Not a good idea - the SV isn't yours.