in reply to What does perl stack OPs really do?
Does XPUSHs make a copy of my_sv, or it really pushes my_sv into stack?
It really pushes it onto the stack.
#define XPUSHs(s) STMT_START { EXTEND(sp,1); (*++sp = (s)); } STMT_END
Your code has a memory leak, though. The scalar won't get freed since you didn't mortalise it, meaning you didn't tell Perl the stack owns a reference to it.
SV* my_sv = newSViv(12345); XPUSHs(sv_2mortal(my_sv));
Same thing:
SV* my_sv = newSViv(12345); mXPUSHs(my_sv);
It seems if I want to use the poped SV permanently (after FREETMPS and LEAVE), I have to make a copy of return by newSVsv(),
I think you can simple increase its REFCNT. If you do this, it's your responsibility to decrement its REFCNT when you're done with it.
Do I need to free the return manually, or it has been done automatically by FREETMPS and LEAVE?
You just have to call FREETMPS.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: What does perl stack OPs really do?
by llancet (Friar) on Jun 21, 2011 at 04:34 UTC | |
by Anonymous Monk on Jun 23, 2011 at 09:48 UTC | |
by llancet (Friar) on Jun 25, 2011 at 01:52 UTC | |
by llancet (Friar) on Jun 25, 2011 at 02:20 UTC |