Subject: [ruby-ffi] Re: Iterating a linked list |
From: Wayne Meissner |
Date: 5/7/12 5:26 PM |
To: ruby-ffi@googlegroups.com |
Hello,
I recently started using FFI and I have to say it is one of the best libraries I have ever used. Very neat syntax and performance. Thanks for such a great library.
Everything was working fine for me untill I got to iterate over a linked list. I am getting a segmentation fault when used from FFI. My C code works well and all tests are passing. My C function is like,
extern int
get_all_tokens(
object *handle,
int token_type,
struct token **tokens
);
This function points the head of the linked list to "tokens" variable. Here is my FFI code.
token_ptr = FFI::MemoryPointer.new :pointer
done = Library.get_all_tokens($handle.get_pointer(0), 1, token_ptr);
if done != 0
error_message = Library.get_last_error($handle.get_pointer(0))
error error_message
return
end
ptr = token_ptr.read_pointer
until ptr.null?
item = Library::Token.new(ptr)
puts item[:pattern] ----> Fails here
ptr = item[:next]
end
My C structure looks like,
struct token {
int type, match_type;
char tag[TOKEN_TAG_MAX];
char pattern[SYMBOL_MAX];
char value1[SYMBOL_MAX];
char value2[SYMBOL_MAX];
int children;
struct token* next;
};
And here is the FFI equivalent one,
class Token < FFI::Struct
layout :type, :int,
:match_type, :int,
:tag, :string,
:pattern, :string,
:value1, :string,
:value2, :string,
:children, :int,
:next, :pointer
end
When running this program, I am getting
[BUG] Segmentation fault
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
Any clue to resolve this issue would be helpful.
Thanks
Navaneeth