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

From: Ben Simon (benjisimon at gmail.com)
Date: Thu Dec 25 18:29:10 EST 2008

On Thu, Dec 25, 2008 at 4:49 PM, Eli Barzilay <eli at barzilay.org> wrote:

> On Dec 25, Ben Simon wrote:
> > 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.)


Yeah, that sounds totally logical.


> > 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.


Cool - Thanks!  I'll give that a try and see how it works.

-Ben



-- 
Have an idea for software?  I can make it happen -
http://www.ideas2executables.com
My Blog: http://benjisimon.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20081225/af906934/attachment.html>

Posted on the users mailing list.