in reply to Re^2: PerlXS typemap and reference counting
in thread PerlXS typemap and reference counting

I need to increase the reference count and this is the part I don't know how to do in XS

As BrowserUK indicated, you can use SvREFCNT_inc/SvREFCNT_dec (see perldoc perlapi) to increment/decrement the reference count in XS, or you can write perl wrappers for those XS functions if you want to do the manipulating from perl space:
use strict; use warnings; use Devel::Peek; use Inline C => Config => CLEAN_AFTER_BUILD => 0, BUILD_NOISY => 1; use Inline C => <<'EOC'; void incref(SV * sv) { SvREFCNT_inc(sv); } void decref (SV * sv) { SvREFCNT_dec(sv); } EOC my $str = 'hello world'; Dump $str; print "\n"; incref $str; Dump $str; print "\n"; decref $str; Dump $str;
The REFCNT of those 3 dumps is 1, 2, and 1 respectively.

I think I am better off refactoring parts of the XS code to perl and have the perl interpreter manage the reference counting

That wouldn't be my first choice, but it's certainly an option - especially if it's proving difficult to handle the reference counting correctly.

Cheers,
Rob

Replies are listed 'Best First'.
Re^4: PerlXS typemap and reference counting
by BrowserUk (Patriarch) on Apr 24, 2016 at 12:27 UTC

    FYI: Core module, Internals has GetRefCount() & SetRefCount(). {thinks for 10 seconds and adds...} but demoing I::C at this point is a very good idea ;)


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.