Don't ask to ask, just ask | |
PerlMonks |
Re: OO Inline::C - returning $self not workingby sfink (Deacon) |
on Mar 08, 2006 at 03:35 UTC ( [id://535096]=note: print w/replies, xml ) | Need Help?? |
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: (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 Section
Seekers of Perl Wisdom
|
|