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

In reply to Re^2: XS: Manipulating refcounts by creamygoodness
in thread XS: Manipulating refcounts by creamygoodness

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.