Subject: Re: [ruby-ffi] Initialize FFI::Pointer in C? |
From: Wayne Meissner |
Date: 12/9/09 1:04 AM |
To: ruby-ffi@googlegroups.com |
Really, don't use the C api - it will break at some point, since it is not intended in any way to be a public api. Use a FFI callback to call from C to ruby. http://wiki.github.com/ffi/ffi/examples has a callback example, as does http://wiki.github.com/ffi/ffi/windows-examples 2009/12/9 Simon Chiang <simon.a.chiang@gmail.com>:
I'm building a callback to ruby from an extension and I would like to pass back pointers much like an attached function that returns a :pointer. Basically something like this: int RubyCallback(void *thing) { VALUE ffi_pointer = rbffi_Pointer_NewInstance(thing); VALUE ruby_module = rb_const_get(rb_cObject, rb_intern ("SomeModule")); VALUE ruby_result = rb_funcall(ruby_module, rb_intern("callback"), 1, ffi_pointer); long result = FIX2INT(ruby_result); return(result); } And then in ruby: module SomeModule module_function def callback(ptr) ptr.inspect # => shows this to be an FFI::Pointer end end A method like this works on OS X with ffi-0.5.0 (pre snow-lepoard, I'll get the version if necessary) but results in a segfault on snow leopard 10.6.2. The segfault is triggered upon inspect. I don't know how FFI works... I really only got this working by taking a guess at what rbffi_Pointer_NewInstance does. Any ideas what's going wrong on 10.6.2 and/or how I can do this better?