Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: XS: Manipulating refcounts

by mugwumpjism (Hermit)
on Sep 19, 2006 at 06:00 UTC ( [id://573637]=note: print w/replies, xml ) Need Help??


in reply to XS: Manipulating refcounts

You're already doing all the right things - looking at the objects with Devel::Peek, storing a blessed IV, etc. Nice work. This is similar to a module I maintain, Set::Object.

I think all that's happening is that at global destruction time, structures aren't cleaned up in the normal way. After all, you still want destructors for objects in circular references to be called. Instead Perl just calls destructors for all objects, and it wouldn't surprise me if it did that without dropping refcnt values first.

Perhaps try changing your test program to trigger the objects falling out of scope before the end of the program is reached.

I also remember having to use the sv2mortal range of macros, perhaps have a look at those too if the above doesn't help.

$h=$ENV{HOME};my@q=split/\n\n/,`cat $h/.quotes`;$s="$h/." ."signature";$t=`cat $s`;print$t,"\n",$q[rand($#q)],"\n";

Replies are listed 'Best First'.
Re^2: XS: Manipulating refcounts
by creamygoodness (Curate) on Sep 19, 2006 at 06:56 UTC

    Thanks for the kind words, and for picking up the gauntlet, Sam. (I presume you're Sam Vilain, since that's who's released the last several versions of Set::Object.)

    I think all that's happening is that at global destruction time, structures aren't cleaned up in the normal way. After all, you still want destructors for objects in circular references to be called. Instead Perl just calls destructors for all objects, and it wouldn't surprise me if it did that without dropping refcnt values first. /

    I'd be cheesed if we were getting all the way through to global destruction! The problem is that Artist::DESTROY is getting called earlier -- specifically, when the $artist scalar is assigned a new value, obliterating the last perl-space reference.

    (Your cautionary advice about order of object destruction at globo-destruct-time is taken under advisement nonetheless. If you really need cleanup to proceed according to a strict itinerary, explicit methods are the way to go.)

    I also remember having to use the sv2mortal range of macros, perhaps have a look at those too if the above doesn't help.

    FWIW, using Inline C means using sv_2mortal() lots. :) Anytime you return an SV* from an Inline C function, it creates an XS wrapper that calls sv_2mortal() on your return value. So it better have a reference count of at least one or Perl will attempt a double free! For illustration, here's the wrapper generated by Inline::C for get_name_from_artist...

    XS(XS_main_get_name_from_artist) { dXSARGS; if (items != 1) Perl_croak(aTHX_ "Usage: main::get_name_from_artist(artist_sv) +"); PERL_UNUSED_VAR(cv); /* -W */ { SV * artist_sv = ST(0); SV * RETVAL; RETVAL = get_name_from_artist(artist_sv); ST(0) = RETVAL; sv_2mortal(ST(0)); } XSRETURN(1); }
    If I call sv_2mortal() on a return val from a function which is currently returning an SV with a refcount of 1, Perl tries the double free -- bad!

    I think my problem arises from conceptual confusion as to which SV* Perl considers the "object"... Despite the fact that in the Devel::Peek Dump, the inner SV* is labeled an OBJECT and has the package attached, it seems that Perl considers that outer SV* the object, and so that's whose refcount has to be incremented. The question is how to do that without creating a circular ref.

    --
    Marvin Humphrey
    Rectangular Research ― http://www.rectangular.com
      Anytime you return an SV* from an Inline C function, it creates an XS wrapper that calls sv_2mortal() on your return value.

      I don't think this has anything to do with Inline::C. I think it's something that xsubpp does in creating the C file from the XS file. If I'm not mistaken, you'll get exactly the same thing in a normal XS environment (ie even of you're not using Inline::C).

      Cheers,
      Rob

        Yep. It was imprecise of me to attribute the action to Inline::C, when in reality the action is performed by xsubpp on orders from Inline::C.

        --
        Marvin Humphrey
        Rectangular Research ― http://www.rectangular.com

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://573637]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-29 13:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found