[plt-scheme] scheme/foreign issues - string conversion and DrScheme crashing

From: Eli Barzilay (eli at barzilay.org)
Date: Thu Dec 25 16:49:19 EST 2008

On Dec 25, Ben Simon wrote:
> On Thu, Dec 25, 2008 at 3:56 PM, Eli Barzilay <eli at barzilay.org> wrote:
> > On Dec 25, Ben Simon wrote:
> >
> > > 2) DrScheme has been crashing nearly constantly since I started
> > > working on this.
> >
> ...
> I wonder if I'm hosing things up because I'm calling mysql_init without an
> argument. The API says that if the argument is null, it returns back a fresh
> mysql handle.  That's what I was intending.  But, by not explicitly handing
> in NULL, i may be corrupting matters.

That sounds very likely: if you omit the type from the interface, then
the foreign function will just use whatever junk happens to be on the
stack.  And it sounds like mysql will just assume that it's a pointer
to a struct to fill -- so it stores that information in that random
place.  (And it shouldn't be surprising that this leads to a crash at
some later point.)

> How do I rewrite:
>   (define c-mysql-init (get-ffi-obj  "mysql_init" libmysql (_fun ->
> _handle)))
> so that the function takes in an explicit NULL?

Probably something like:

  (define c-mysql-init
    (get-ffi-obj  "mysql_init" libmysql (_fun _pointer -> _handle)))

and pass it #f (which translates to NULL).  But you can also make it
always pass in NULL with:

  (define c-mysql-init
    (get-ffi-obj  "mysql_init" libmysql (_fun [_pointer = #f] -> _handle)))

which makes the generated Scheme function expect no arguments, and
always call the foreign one with #f.

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.