Subject:
[ruby-ffi] Initialize FFI::Pointer in C?
From:
Simon Chiang
Date:
12/9/09 12:09 AM
To:
ruby-ffi

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?