Subject:
[ruby-ffi] Re: **char issues
From:
Wayne Meissner
Date:
10/28/09 8:03 PM
To:
ruby-ffi@googlegroups.com


2009/10/29 bk <bkearney@redhat.com>:
>
> I am attempting to convert the augeas bindings to ffi. I did this with
> JNA, and it was very helpful. The method in question is:
>
> int aug_get(const augeas *aug, const char *path, const char **value);
>
> where the last paraemeter returns the result of the get. I have
> attempted to wrap this with
>
>  attach_function :aug_get, [:pointer, :string, :pointer], :int
>
> and then call this with:
>
>    def get(path)
>        ptr = FFI::MemoryPointer.new(:pointer, 1)
>        AugeasLib.aug_get(@aug, path, ptr)
>        strPtr = ptr.read_pointer()
>        return strPtr.read_string()
>    end
>
> This works fine if the get returns something, but if the get ruturns a
> null string I fail with:
>
> FFI::NullPointerError: invalid memory read at address=(nil)

You need to test strPtr for null
e.g.

  if !((strPtr = ptr.read_pointer).null?
    strPtr.read_string
  else
    ""  # Or whatever end value you want
  end