Subject:
Re: [ruby-ffi] Pointer to a struct - how to cast?
From:
Jeremy Voorhis
Date:
5/20/10 11:37 AM
To:
ruby-ffi@googlegroups.com

The code looks alright to me on first glance. Are you sure your pointer is valid?

On Thu, May 20, 2010 at 8:14 AM, Drew <drew.avis@gmail.com> wrote:
Hi there, I have a pointer to a struct that looks like this:

// c:
typedef struct
       {
        IntT offset;
        IntT dataType;
        union {
               StringT sdata;
               IntT idata;
               } u;
       } F_TextItemT;

In Ruby I've defined it like this:

 class FTextItemU < FFI::Union
   layout :sdata, :string,
   :idata, :int
 end

 class FTextItemT < FFI::Struct
   layout :offset, :int,
     :dataType, :int,
     :u, FTextItemU
 end

I'm trying to cast the pointer like this:

my_struct = FM::FTexFtItemT.new(p)
puts "test: #{my_struct[:dataType]}"

However, I'm getting a seg fault on the puts statement.

Any ideas of what I'm doing wrong?

Thanks!
Drew