Clear questions and runnable code
get the best and fastest answer
Re^5: XS: EXTEND/mPUSHiby ikegami (Pope)
|on Sep 27, 2011 at 01:08 UTC||Need Help??|
Indeed, there's no leak.
Perl's stack isn't refcounted, so that means you can't decrement the refcount of a scalar you place on the stack even if you don't keep a reference to it.
What you do is place the scalar on a list of variables whose refcount needs to be decremented when they are removed from the stack. Variables on this list are called mortals.
(This way of doing things is a source of many bugs, but that's the box you have to play in.)
The "m" in "mPUSHi" stands for mortal. The created scalar will be made mortal.
boils down to
Check the first value being returned. It's always your argument (n).
As documented EXTEND will make sure there is n spaces available. That's it. It doesn't remove anything already there.
In your case, you end up with at least n+1 spaces on the the stack total, one of which is used for the argument.
It's actually added by the XS to C converter, xsubpp. Inline::C doesn't put there. In fact, Inline::C specifically doesn't want it, but doesn't have a way of telling xsubpp not to call it.
Inline::C requires that you call PUTBACK (explicitly or via XSRETURN) because it can't do it for you because it doesn't have access to your sub's SP to "put it back".
Real XS doesn't require that you call PUTBACK because xsubpp actually creates the sub, so it has access to the sub's SP.
PS — C doesn't require the use of return; when the return type is void.