Jan Dubois <[EMAIL PROTECTED]> writes:
>On Sun, 07 Sep 2003 09:49:32 +0200, Tassilo von Parseval
><[EMAIL PROTECTED]> wrote:
>
>>Hi there,
>>
>>this is the strangest thing I've ever encountered so far with XS. To me
>>it looks like a very obscure bug in perl, but hopefully it's not. The
>>Perl examples actually use autobox, but the very same behaviour shows up
>>when using a pure functional interface:
>>
>>I have a very basic C function that triggers a Perl callback:
>>
>> SV * call_Pslice (const char *func) {
>> SV *res;
>>
>> dSP;
>> ENTER;
>> SAVETMPS;
>> PUTBACK;
>>
>> (void)call_pv(func, G_SCALAR);
>> SPAGAIN;
>>
>> res = newSVsv(POPs);
>>
>> PUTBACK;
>> FREETMPS;
>> LEAVE;
>>
>> return sv_2mortal(res);
>> }
>
>You are missing a PUSHMARK(xxx) call in here. The call_pv() will pop your
>callers frame from the stack, and you caller doesn't seem to adjust for
>it.
>
>Two more unrelated notes:
>
>1) You call sv_2mortal() on your result without checking if the result has
>the SvREADONLY bit set. If the sub can return undef, you may be modifying
>the refcount of an immortal, which doesn't matter in most cases as it
>starts with a refcount of 2**31, but is still bad style (IMO).
I am not that bothered by that style issue. A related issue
is that the value POPs-ed was a return value which would have been just
fine a 'res' - if you removed the SAVETMPS/FREETMPS which will free it.
>
>2) Calling call_xxx() without G_EVAL often doesn't work correctly when
>your Perl sub can die(). Maybe it is just cargo cult, but I've been
>bitten by this one too many times and now always specify G_EVAL and check
>SvTRUE(ERRSV) myself to handle exceptions myself.
This has (I think) finally been fixed - but only recently.
Stick with the cargo-cult for portability.