----- Original Message ----- 
From: "Nicholas Clark" <[EMAIL PROTECTED]>
To: "Mark T. Kennedy" <[EMAIL PROTECTED]>
Cc: <inline@perl.org>
Sent: Sunday, August 06, 2006 11:38 PM
Subject: Re: calling perl from Inline::C


> On Wed, Aug 02, 2006 at 02:35:06PM -0400, Mark T. Kennedy wrote:
>
> > however, the callback in the complex program SEGV's within
> > 'call_pv'. here is the  source that causes the crash:
> >
> > void pass_to_perl (void *p)
> > {
> >   Inline_Stack_Vars;
> >   Inline_Stack_Push (newSVpvf ("this is an arg string"));
>
> you probably don't want to be using newSVpvf here. It's akin to using C's
> fprintf() when really you wanted fputs() (with all the same rogue format
> string errors.
>

Actually, when I think back to when I spent time messing with callbacks in
Inline::C, I can't be certain that I ever *did* get a segfault. I know I got
garbled output on occasions, and 'attemt to free unreferenced scalar' on
occasions ... but I can't be certain that I ever got a segfault.

Here's an example of garbled output, based on the callback example in the
cookbook. I've replaced the 'newSVpvf' (which the cookbook uses) with
'newSVpv' - though reverting to newSVpvf() doesn't have any effect on
behaviour for me:

---------------------------------------------
use warnings;
use Inline C;

$x = 1;

for (1..$x){
    foo_1('This is the first line');
    foo_1('This is the second line');
    print "###################\n";

}

for (1..$x){
    foo_2('This is the first line');
    foo_2('This is the second line');
    print "###################\n";

}

sub perl_sub_1 {print "@_\n"}

__DATA__
__C__

void foo_1(SV* text) {
     dSP;
     ENTER;
     SAVETMPS;
     XPUSHs(newSVpv("Plus an extra line", 0));
     PUTBACK;
     call_pv("main::perl_sub_1", 0);
     FREETMPS;
     LEAVE;
}

void foo_2(SV* text) {
     Inline_Stack_Vars;
     Inline_Stack_Push(newSVpv("Plus an extra line", 0));
     Inline_Stack_Done;
     call_pv("main::perl_sub_1", 0);
     Inline_Stack_Void;
}

---------------------------------------------

Both foo_1() and foo_2() are intended to produce the same output, but only
foo_1() works as intended - the foo_1() code is based on what we find in
'perldoc perlcall', the foo_2() code uses the Inline_Stack_Vars (as per the
cookbook). There's no problem with either rendition if they're not called in
a for loop. But, called in a for loop, foo_2() produces garbled output.

Is it possible to write foo_2() using *only* the Inline_Stack_Vars, such
that it works as desired in a for loop ? I couldn't find a way of doing it.

Here's the output I get:

F:\pscrpt\inline>perl try.pl
This is the first line Plus an extra line
This is the second line Plus an extra line
###################
1 1 This is the first line Plus an extra line
Plus an extra line
 This is the second line Plus an extra line
###################

You can set the value of $x to greater than one, of course .... and you'll
see that foo_2() continues to produce garbled output at each iteration, and
foo_1() produces desired output at each iteration.

Cheers,
Rob


Reply via email to