Subject:
Re: [ruby-ffi] am I misusing :pointer in a FFI::Struct?
From:
Wayne Meissner
Date:
8/16/10 3:49 PM
To:
ruby-ffi@googlegroups.com

Thats a bug in FFI::Pointer#inspect ... or more to the point, it
probably does not define it (whereas JRuby does) - so you're getting
the default ruby behaviour which is to print out the object's type and
the object's address (which is not the pointer address, but ruby's
internal address of the object itself).

If you print out foo[:ptr].address it should all look the same.

Can you file a bug in ruby-ffi's tracker, so this gets fixed?


On 17 August 2010 04:26, Chuck Remes <cremes.devlist@mac.com> wrote:
> 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]
>
>
>
>
>
>
>