[racket] FFI Pointing to racket objects

From: Sam Vervaeck (vervaeck.sam at skynet.be)
Date: Sat Mar 2 10:35:58 EST 2013

Hi,

 

I am forking an FFI library for physics simulation on Github
(https://github.com/samvv/Rhipmunk-Physics) but I got stuck with storing
racket objects in the library's native structs. From the library's website:
http://chipmunk-physics.net/release/Chipmunk-6.x/Chipmunk-6.1.2-API-Referenc
e/group__cp_body.html. Here's the part of the code that needs to be changed:

 


(define _cpDataPointer _racket)

(define-cstruct _cpBody

    .

    [data _cpDataPointer] ; user-defined data, like a game object

    .)

 

In the following test everything seems to work as expected:

 

(define body1 (cpBodyNew 1.0 1.0))

(set-cpBody-data! body1 "This is a test")

(display (cpBody-data body1))

> "This is a test"

(set-cpBody-data! body1 (cons 1 2))

(display (cpBody-data body1))

> (1 . 2)

 

However, when I run the following code:

 

(collect-garbage)

(display (cpBody-data body1))

 

Something goes wrong internally. Sometimes DrRacket crashes, sometimes it
returns values like #<bad-value>. I have looked into (malloc-immobile-cell
v) and (free-immobile-cell cptr) but I am not sure that fiddling with the
memory is such a good idea. Could you please give me any guidelines or point
me to a procedure I can use to provide this functionality?

 

Thanks,

 

Sam

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130302/35276b4c/attachment.html>

Posted on the users mailing list.