Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Inline C: using stderr segfaults?

by mwah (Hermit)
on Nov 05, 2007 at 12:26 UTC ( [id://649003]=note: print w/replies, xml ) Need Help??


in reply to Inline C: using stderr segfaults?

Microsoft (R) 32-bit C/C++ Standard Compiler Version 13.00.9466 for 80x86

Maybe thats the point.

Wasn't Activestate Perl build with the 12.xx version?

Using your code, I got a clean run under AS 822 and

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86

where it says:

  Got:'fred'
  77c2fcc0
  Got:'fred'

Regards

mwa

Replies are listed 'Best First'.
Re^2: Inline C: using stderr segfaults?
by BrowserUk (Patriarch) on Nov 05, 2007 at 12:59 UTC

    You may well be right I fear, though this is the first time I have encountered a problem with the apparent mismatch. I tracked the error using a debugger to an Rtl CriticalSection function.

    And the following version which crudely approximates using fprintf() runs fine:

    #! perl -slw use strict; use Inline 'FORCE'; use Inline C => 'DATA', NAME => 'IC_junk1', CLEAN_AFTER_BUILD => 0; test( 'fred' ); test2( 'fred' ); __DATA__ __C__ //#include <stdio.h> void test ( char* text ) { printf( "Got:'%s'\n", text ); printf( "%x\n", stderr ); } void test2 ( char* text ) { char buf[ 255 ]; sprintf( buf, "%s", text ); fputs( stderr, buf ); }

    So it does look like some difference in the CRTs that is responsible. I'd swear that I've used fprintf() from inline C before, but a quick scan of my files revealed none and I found a couple that called Perl_warn(), so maybe I encountered and worked around it some time ago and forgot.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      So it does look like some difference in the CRTs that is responsible

      It need not exactly be a "difference in the CRTs". When perl uses one CRT and the compiler uses a different CRT, you can get situations where something (eg a handle) is created by one CRT and then passed to the other CRT (which knows nothing about that handle, and segfaults).

      This problem arises when you try to build Win32::SharedFileOpen - which is the only example I know of, btw. A little surprising (at first glance) is the fact that you can build Win32::SharedFileOpen on ActivePerl using the MinGW compiler ... but it's not really surprising, because MinGW uses the same CRT as ActivePerl.

      Fwiw, the original script you provided builds fine for me on ActivePerl using MinGW.

      Cheers,
      Rob

        There does appear to be a bug in the impementation of fprintf(). Note how in the output of the following code, test() prints the value of stderr when fetched via the Perl macros, and directly from the OS and gets the same value. But then in test2(), when it tries to pass & use the directly accessed value with fprintf() it does in exactly the same way and at exactly the same point as when using it via the Perl macro.

        However, when I pass the Perl macro derived value to vfprintf() in eprintf() it works fine. Indicative of something, though I'm not sure exactly what :)

        #! perl -slw use strict; #use Inline 'FORCE'; use Inline C => 'DATA', NAME => 'IC_junk1', CLEAN_AFTER_BUILD => 0; test( 'fred' ); test2( 'fred' ); __DATA__ __C__ #include <stdio.h> #include <stdarg.h> void eprintf (const char *template, ...) { va_list ap; va_start( ap, template ); vfprintf( stderr, template, ap ); va_end( ap ); } void test ( char* text ) { printf( "Got:'%s'\n", text ); printf( "stderr via PerlIO macro: %x\n", stderr ); printf( "stderr via Win32_stderr: %x\n", win32_stderr() ); } void test2 ( char* text ) { // eprintf( "Get:'%s'\n", text ); fprintf( win32_stderr(), "Get:'%s'\n", text ); } /* Outputs: C:\test>IC-junk1.pl Got:'fred' stderr via PerlIO macro: 77c5acc0 stderr via Win32_stderr: 77c5acc0 then segfaults when fprintf( stderr, .... ) is called. */

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://649003]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-19 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found