in reply to Re: XS: free() outside of the main thread causes crash on Windows
in thread XS: free() outside of the main thread causes crash on Windows

Some perl distributions for windows includes pthreads-win32 implementation of pthreads. Strawberry Perl, I think.

And for others I built pthreads-win32 myself and changed Makefile.PL a little bit. You can find fully compilable version for both windows and others OS here: https://github.com/olegwtf/sandbox/tree/master/xs-free-windows-problem
  • Comment on Re^2: XS: free() outside of the main thread causes crash on Windows

Replies are listed 'Best First'.
Re^3: XS: free() outside of the main thread causes crash on Windows
by BrowserUk (Patriarch) on Sep 25, 2014 at 07:32 UTC

    I strongly suggest that you should compile your code with /E or equivalent to produce the post-preprocessor code and take a look at what is actually being call for malloc() and free(); because it almost certainly isn't the appropriate CRT routines.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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.
      It says that free() is win32_free() and malloc() is win32_malloc().

        If you look into win32.c/.h and unwind those functions, you'll find that they use a custom (pseudo-C++ style) allocator that uses different pools for different threads. (iThreads, that is.)

        But for that to work properly, it needs to be initialised and as you are not instantiating an interpreter in your thread(s), you are not getting that initialisation done.

        Basically, by using the perl environment to compile (what is essentially) pure C code, you're just opening yourself to a whole world of hurt. It, the Perl environment, simply isn't designed to be used that way.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        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.
        If you do this on win32 I don't get this "double free" error
        #include <pthread.h> void *thread(void *arg) { char *msg = (char*)arg; printf("thread: %s\n", msg); free(msg); return NULL; } #include "EXTERN.h" ...
        But yes perl does redefine these things, ...

        The problem is that those also get redefined at various points. See win32\win32iop.h. Sometimes, multiple times depending upon where you are in the source code.

        And the allocator used under Windows is a custom allocator. See win32\vmem.h. (Though that is also dependent upon build-time options...)

        It's all very convoluted to work out exactly what is being compiled.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        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.
Re^3: XS: free() outside of the main thread causes crash on Windows
by Anonymous Monk on Sep 25, 2014 at 07:26 UTC
    Have you heard of perlclib? It advocates using Newx() and Safefree() instead of malloc/free
      Yes. I made following changes:
      -free(msg); +Safefree(msg); -char *thread_arg = malloc((strlen(msg)+1)*sizeof(char)); +char *thread_arg; +Newx(thread_arg, strlen(msg)+1, char);
      But this didn't help. Still crashes