[racket] FFI on Windows

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Nov 27 18:59:22 EST 2011

At Sun, 27 Nov 2011 18:20:46 -0500, Harry Spier wrote:
> Dear list members,
> On going through the article on the FFI by Eli Barzilay and Dmitry Orlovsky
> http://www.ccs.neu.edu/scheme/pubs/scheme04-bo.pdf
> It says:
> "libffi is maintained and distributed as part of the GCC project,
> but its goal is to provide a portable library. We use it for all
> platforms that PLT Scheme targets, including Windows (using a
> slightly adapted version that works with Microsoft’s compiler, courtesy
> of the Thomas Heller [13])."

(Racket now uses the main libffi sources for Windows, too.)

> Does that mean that if I'm using the Windows version of Racket and I want
> to use the FFI to interface to ImageMagick, that the ImageMagick Windows
> executable must also be compiled with the microsoft C compiler rather than
> with  MingW GCC ?


> A few years ago when I interfaced ImageMagick to Ruby using Ruby's
> ImageMagick interface "RMagick" there was a problem for a while about
> RMagick being compiled with Microsoft Visual C compiler (it may have been a
> particular version of the Visual C compiler?)  and I think the ImageMagick
> executable for windows was compiled with MingW GCC.  The problem was
> resolved when the RMagick interface was compiled using the same compiler as
> ImageMagick.

That sounds like an issue with mismatched C runtime libraries.

The main way a mismatch can show up with Racket's FFI is when a foreign
function allocates a value, and the caller is supposed to free it with
free(). In that case, calling the Racket `free' function may not work,
because it may use a different memory allocator from the one linked to
the foreign library --- i.e., different instances of malloc() and
free() from different C libraries.

Most libraries that provide allocation functions also provide
corresponding deallocation functions instead of relying on free(), and
that avoids the problem. Meanwhile, Racket uses "msvcrt.dll" as its C
runtime library, which is consistent with most binary distributions of
open-source software. I believe that MinGW binaries by default use

For all of those reasons, you probably won't run into problems due to a
compiler mismatch when using Racket's FFI.

Posted on the users mailing list.