Subject:
[ruby-ffi] am I misusing :pointer in a FFI::Struct?
From:
Chuck Remes
Date:
8/16/10 1:26 PM
To:
ruby-ffi

I have an FFI::Struct that contains a field of type :pointer. When I assign an FFI::Pointer to that field, I get very different behavior from MRI versus JRuby. A small script shows what I mean.

Note that under 1.9.1 and 1.9.2, the value of foo[:ptr] changes each time it is accessed. Under JRuby it reports the correct pointer value every time.

The 'ffi' gem was built from git master.

Am I just using it wrong or is this a bug in the C extension for MRI?

cr


cremes$ cat u.rb
require 'ffi'

class Foo < FFI::Struct
  layout :ptr,  :pointer
end

class Bar < FFI::Struct
  layout :value, :short
end

foo = Foo.new
bar = Bar.new
bar[:value] = 23
puts "bar.to_ptr #{bar.to_ptr}"
a = bar.to_ptr
puts "a #{a}"
foo[:ptr] = bar.to_ptr

puts "ptr #{foo[:ptr]}, #{foo[:ptr]}, #{foo[:ptr]}"


OUTPUT

Mac-Pro:tmp cremes$ ruby -v
ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-darwin10.2.0]
Mac-Pro:tmp cremes$ ruby u.rb
bar.to_ptr #<FFI::MemoryPointer:0x000001018d8c00>
a #<FFI::MemoryPointer:0x000001018d8c00>
ptr #<FFI::Pointer:0x000001018d8ab0>, #<FFI::Pointer:0x000001018d8a08>, #<FFI::Pointer:0x000001018d8960>
Mac-Pro:tmp cremes$ rvm use 1.9.2-head

info: Using ruby 1.9.2 head
Mac-Pro:tmp cremes$ ruby u.rb
bar.to_ptr #<FFI::MemoryPointer:0x0000010101d900>
a #<FFI::MemoryPointer:0x0000010101d900>
ptr #<FFI::Pointer:0x0000010101d7c0>, #<FFI::Pointer:0x0000010101d748>, #<FFI::Pointer:0x0000010101d6d0>
Mac-Pro:tmp cremes$ rvm use jruby

info: Using jruby 1.5.1
Mac-Pro:tmp cremes$ ruby u.rb
bar.to_ptr #<MemoryPointer address=0x10220dc20 size=2>
a #<MemoryPointer address=0x10220dc20 size=2>
ptr Pointer [address=10220dc20], Pointer [address=10220dc20], Pointer [address=10220dc20]