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

From: Ben Simon (benjisimon at gmail.com)
Date: Thu Dec 25 14:41:36 EST 2008


With the recent chatter about MySQL on this list and some dabbling I've been
doing with FFI (
I thought, what the heck, why not whip up a quick FFI interface to mysql.

I started in on it, and I've run into two major issues:

1) the MySQL C api (http://dev.mysql.com/doc/refman/5.0/en/c.html) is easy
to use.  I'm able to get access to the C function call *mysql_real_connect*.
If I hand it invalid connection information, it gracefully fails like I want
it to. It calls the C function *mysql_error()* which is supposed to return
back char*.  However, I get the following message from DrScheme:

 bytes->string/utf8: string not well-formed UTF-8 encoding: #"0\344\22"

Any thoughts as to why *mysql_error* is returning back a string scheme is
choking on?

2) DrScheme has been crashing nearly constantly since I started working on
this.  I'm running on Vista, and after having DrScheme open for a minute or
two, I'll inevitably get a pop-up: *PLT Scheme GUI Application has stopped
working*.  It doesn't seem to correspond to any one statement I'm
executing.  For example, I kicked off DrScheme, ran the test code mentioned
above, got the error, started writing this e-mail, and flipped back to find
DrScheme had crashed.

I'm running

Could this be a pre release issue?

Anyone have any ideas as to how I could get the specific cause of the crash?

This is probably just the result of some poorly written FFI code (which, I
realize, is unsafe).  Perhaps there's a glaring bug in the code below?

Here's the code I've worked up this far - For now, I'm testing it by running
the (do-test) procedure.



(require scheme/foreign)

(define current-mysql-handle (make-parameter #f))

(define libmysql (ffi-lib "libmysql"))

(define _handle (make-ctype _pointer #f
                                    (when handle (register-finalizer handle

(define _data (make-ctype _pointer #f
                          (lambda (data)
                            (if data
                                (error 'mysql (c-mysql-error))))))

(define-fun-syntax _handle*
  (syntax-id-rules ()
    [_ (type: _handle expr: (current-mysql-handle))]))

(define c-mysql-init (get-ffi-obj  "mysql_init" libmysql (_fun -> _handle)))
(define c-mysql-close (get-ffi-obj "mysql_close" libmysql (_fun _handle ->
(define c-mysql-error (get-ffi-obj "mysql_error" libmysql (_fun _handle* ->

(current-mysql-handle (c-mysql-init))

(define c-mysql-real-connect (get-ffi-obj "mysql_real_connect"
                                          (_fun _handle* _string _string
_string _string _int
                                                _string _long -> _data)))

(define (do-test)
  ; Intentionally fail to connect, if all goes well, raise a scheme error
  (c-mysql-real-connect "localhos" "root"  "invalid" "none" 0 "" 0))

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

Posted on the users mailing list.