[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

Howdy,

With the recent chatter about MySQL on this list and some dabbling I've been
doing with FFI (
http://benjisimon.blogspot.com/2008/11/small-experiment-with-mzschemes-foriegn.html),
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  4.1.3.6-svn25dec2008.

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.

Thanks,
Ben

------------------------------------------------------------------------------------------------------------------------------------------------

(require scheme/foreign)
(unsafe!)

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

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

(define _handle (make-ctype _pointer #f
                                  (lambda
(handle)
                                    (when handle (register-finalizer handle
c-mysql-close))
                                    handle)))

(define _data (make-ctype _pointer #f
                          (lambda (data)
                            (if data
                                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 ->
_void)))
(define c-mysql-error (get-ffi-obj "mysql_error" libmysql (_fun _handle* ->
_string)))

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


(define c-mysql-real-connect (get-ffi-obj "mysql_real_connect"
                                          libmysql
                                          (_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 -
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/f9053739/attachment.html>

Posted on the users mailing list.