The problem is the reference counts.

XS implicitly decrements the reference count of any returned SV* (but not returned AV*'s or HV*'s, which is a bug that we're stuck with because it's part of the API and cannot be changed without breaking existing code.) Which magically works out, usually, because you'll be returning something you just created with newSViv(), and that sets the initial reference count to 1.

In your case, you're returning a parameter, and nothing else is happening to its reference count. So by calling set_name(), you end up discarding a reference to one of your objects after the statement is done.

It doesn't break immediately because it's a deferred reference count decrement -- XS mortalizes the returned SV*. The interpreter will remember how many times you mortalize something, and decrement the refcount that many times after the whole statement is complete.

You have the same problem in get_parent(), although it's a bit obscured because you're declaring Node.parent to be of type 'struct Node*', but you're actually storing (and retrieving) an SV*.

So to fix your code, do SvREFCNT_inc(self) on all of your set_* functions, and to the value you're returning from get_parent(). It still isn't quite correct, though, because you're keeping a reference to an SV* in Node.parent. You really should be maintaining the reference counts for what you store in there, too. I think it would be something like:

SV* set_parent(SV* self, SV* parent) { Node* me = (Node*)SvIV(SvRV(self)); SvREFCNT_inc(parent); if (me->parent) SvREFCNT_dec(me->parent); me->parent = parent; SvREFCNT_inc(self); return self; }
(Also note the order of inc/dec of the parent SVs. It seems odd, but consider what would happen if me->parent == parent.)

Warning: I did test this, but refcounting in Perl still confuses me greatly. So however authoritative I may sound, believe me at your own risk!


In reply to Re: OO Inline::C - returning $self not working by sfink
in thread OO Inline::C - returning $self not working by Anonymous Monk

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.