in reply to XS replacing my c library, not compatible with OS threads

There can definitely be trouble if different threads are linking with different memory managers - there are some "tricks"! I stumbled across a couple points while working on a C/C++ project a few months ago - might or might not help you.

malloc() free() and exit(), etc. live in MSVCRT.dll. That dll is the Window OS'es idea of these standard functions. That dll exists on every Windows system. VC2006 and gcc will link with that library. I guess somewhere along the way the OS guys and the compiler guys diverged in what they were doing and now there are many versions of MSVCRT that might or might not be on a particular Windows system. If for example, you have Visual Studio 2008, then that compiler will by default link with MSVCR90.dll a compiler specific library, not with MSVCRT.DLL.

I don't know what flavor of Perl you are running or how it was built. A binary distribution is going to link with the OS's memory mgmt because who knows whether MSVCRXX will wind up being there or not. Anyway some of your woes may be related to this issue.

  • Comment on Re: XS replacing my c library, not compatible with OS threads

Replies are listed 'Best First'.
Re^2: XS replacing my c library, not compatible with OS threads
by BrowserUk (Patriarch) on Dec 01, 2010 at 10:24 UTC

    Whilst mismatched C runtime libraries is certainly an issue with building XS extensions for use with binary distributions, it is far from being the predominant problem.

    The single biggest problem is trying to work out what actually gets called when you use an *alloc call. There are so many #defines, #undefs, wrapper-functions and dispatch tables that conditionally define and redefine each of these calls, that you need to be a compiler to work out what actually gets called. And with so many levels of indirection and macro redefinition, setting breakpoints within a debugger is almost impossible.

    And as the OP indicated, even if you use the compiler /E switch (or equivalent) to view the post preprocessor output, what you get is so complicated--a simple char *p = malloc( 10 ); decoding to several hundred bytes of tortuously nested, and often duplicated) function calls, casts and parens--that it is still essentially unintelligible.


    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.

      There is at least a small utility distributed with Perl to help expanding the macros without having to talk to gcc /E yourself: expand-macro.pl. Of course that only shows you what the macros expand to, eventually, and not what the C code means.

        Shame it doesn't work with my compiler :(

        Actually doing the expansion isn't hard:

        C:\test>type expand_malloc.c #include "EXTERN.h" #include "perl.h" char *p = malloc( 10 ); C:\test> cl -E -nologo -GF -W3 -MD -Zi -DNDEBUG -Ox -GL -Wp64 -fp:prec +ise -DWIN32 -D_CONSOLE -DNO_STRICT -DHAVE_DES_FCRYPT -DWIN64 -DCONSER +VATIVE -DUSE_SITECUSTOMIZE -DPRIVLIB_LAST_IN_INC -DPERL_IMPLICIT_CONT +EXT -DPERL_IMPLICIT_SYS -DUSE_PERLIO -DPERL_MSVCRT_READFIX -MD -Zi -D +NDEBUG -Ox -GL -Wp64 -fp:precise -DVERSION=\"1.76\" -DXS_VERSION= +\"1.76\" "-IC:\Perl64\lib\CORE" -DHAS_PPPORT_H expand_malloc.c >jun +k.txt

        And if you load up the output file junk.txt, and wade you way down to line: 203,525 (Yes! That really is line: two hundred and three thousand five hundred and twenty five!), you get:

        [203524 lines omitted :)] #line 3 "expand_malloc.c" char *p = malloc( 10 );

        which probably means I've got the wrong set of defines on the command line :(

        Update: Of course. I forgot to include XSUB.h:

        ... #line 4 "expand_malloc.c" char *p = (*(*Perl_IMem_ptr(((PerlInterpreter *)Perl_get_context())))- +>pMalloc)((*Perl_IMem_ptr(((PerlInterpreter *)Perl_get_context()))), +(10));

        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.