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.?
In your sub:you get copy of scalars and not references to them. Not C but Perl will create a copy for you and pass to your subroutine. So you will see reference count in your sub equals to 1 even if you make some references to your scalar before calling your sub.SV* test( SV *a, SV *b ) {
You make quite inconsistent thing in your C subroutine, at the very first step. You either replace SV *a with a new SV or do not, based on its constant-ness. Its a hole for scalar leaks. And may be your comment "// Why do I need to increment the ref count?" will go away. What will happen if you do not do "REFCNT_inc" exactly? Will the subroutine work for non-constant scalars?
Why not write following way:
As a side note, Inline::C is perfect for trivial cases, but when dealing with refernce counts and stuff you'll get another layer of hidden mechanics, so you'll have things under better control if you'll go to XS directly.SV* test( SV *a, SV *b ) { SV *c = newSVsv(a); sv_catsv(c,b); return c; }
In reply to Re: XS/Inline::C concat *any* two SVs.
by vkon
in thread XS/Inline::C concat *any* two SVs.
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |