SketchUp crashes when FFI is loaded in Windows 10

Hi,

I’m trying to load the FFI gem version (ffi-1.9.17-x64-mingw32) in Windows 10, Sketchup 2017 (maintenance build - 17.2.2555).

Edit note: In my old post, I had only loaded the ffi_c.so file corresponding to ruby version 2.2 and I can see that was incomplete frm FFI perspective, and hence now loaded the whole of FFI gem.

As I execute the below command of getting a memory pointer, SketchUp throws a bug splat…


$: << File.join('C:\Ruby22-x64\lib\ruby\gems\2.2.0\gems\ffi-1.9.17-x64-mingw32\lib')

require 'ffi'

float_ptr = FFI::MemoryPointer.new :float, 1

But giving the same command outside in ruby IRB in version 2.2.4 creates the pointer just fine.
Any help is greatly appreciated.

Thanks,
Jaya

(1) Instructions on properly formatting error text in the forum ...

Your error report text is incorrectly formatted for the forum. (The forum engine stripped out the error message because it thought it was an invalid HTML element bracketed between < and > characters, which I’ve escaped with leading \ here.)
Please edit (pencil icon) your post, and insert triple backtick delimiter lines, before and after the error text block. (The backtick key is to the left of the 1 key just above the TAB on US keyboards):

```text
# error text here
```


(2) SketchUp comes with it’s own specially compiled edition of Ruby and the standard library.

I would advise you to use the Fiddle library that comes with SketchUp’s Ruby library.

Loading “fiddle.rb” will load “fiddle.so” which loads
SketchUp 20nn/Tools/RubyStdLib/platform_specific/libffi-6.dll
most likely using a LoadLibrary call. Since Ruby is all opensource, you can read the source:
ruby/ext/fiddle at ruby_2_2 · ruby/ruby · GitHub

Thanks for the tip on error text Dan, I have edited my above post.

and on Fiddle,

Yes we are aware of fiddle as alternate, but we have a big library built on top of FFI, and we are in a stage to release the product in Sketchup 2017 but this FFI bug splat is like a show stopper for us now. Re-writing our library in fiddle might take some amount of time, we are trying to see if there is something from our side which can be fixed on this bug splat case.

  1. FFI in 2017+Win 7 combination works just fine,
  2. FFI in 2016 + Win 10 Combination works fine

FFI in 2017 + Win 10 => Bug splat

If this is a Sketchup-only-fix-able issue, we will move on with fiddle option… :worried:

Thanks,
Jaya

FYI, Gems for SketchUp are installed in a SketchUp specific location (purposely kept separate from any “system” Ruby installations.)

From inside SketchUp’s Ruby console you can see the location via:

puts ENV["GEM_HOME"]
puts ENV["GEM_PATH"]

OR

require "pp"
pp ENV

The folder name is “Gems” for 32-bit installations, and “Gems64” for 64-bit installations.


BUT, … this only works well for pure Ruby gems.

SketchUp Ruby and it’s rubygems extension, is not linked up with Ruby’s DevKit, so cannot compile C gems “on the fly” like a normal “system” Ruby install (with the proper DevKit) can.

Sometimes what coders do is provide precompiled 32-bit and 64-bit gems, that are repackaged as a SketchUp extension, or as support libraries within their extension. It may be helpful to tweak the gem namespace(s) so that they are encapsulated inside your toplevel author (or company) namespace module.

We have discussed this both here (in this category) and over at SketchUcation in the Developers forum.

In SU2017 under Win10, I get what looks like a good gem install in SketchUp’s console …

Gem::install "ffi"
#=> [#<Gem::Specification:0xf08816f688 ffi-1.9.25-x64-mingw32>]

require 'ffi'
#=> true

… but then …

float_ptr = FFI::MemoryPointer.new :float, 1
#=> SPLAT!

I sent in a BugSplat report as well referencing this topic thread.
The response said that perhaps SU2018 would fix the issue.

Same crash in SU2018.

Same crash in SU2019…

It would be so nice to communicate with Arduino straight from SketchUp… ‘rubyserial’ requires ‘ffi’.
Well… I guess I will have to wrap C code for this myself.

Using SU 2019 and Ruby 2.7, have the same bug splat with locally built ffi

Ah, never noticed this thread. And we don’t look over all BugSplats submitted.
Best thing to report an issue like this is the bug tracker as that get scrubbed regularly.

I’m surprised that there isn’t a “ffi.rb” wrapper that redirects into Fiddle calls.

I also wonder what is the comparison between Fiddle and FFI gem.

And there is … it is called Fiddley
Once loaded it maps the constant FFI to point at the Fiddley module.

Documentation begins here …
https://www.rubydoc.info/gems/fiddley/0.0.8/toplevel

So @rajasej this may be a better alternative then actually rewriting your code that uses the ffi gem.

@Tomasz, try Fiddley with Arduino and rubyserial and see how it works.

(EDITED: To remove mention of Nokogiri, as it does not depend upon the FFI gem.)


Users of Fiddley need to uninstall the ffi gem. This is because the normal loading call …

require "ffi"

… needs to load "#{ENV["GEM_HOME"]}/gems/fiddley-0.0.8/lib/ffi.rb" instead of the "ffi.rb" that comes with the ffi gem.


Just a note, (after moving the ffi gem to an “off” folder from the "gems" folder, and moving it’s .gemspec archive out of the "specifications" folder,) … I tried the OP’s test using just Fiddley

require 'ffi'
#=> true
float_ptr = FFI::MemoryPointer.new :float, 1
#<Fiddley::MemoryPointer:0x000253fdd9f1b8>
float_ptr.class
#=> Fiddley::MemoryPointer

NO error!

1 Like

Dan,

Works with SU2019 & 2.8.0.

I’m not sure what you mean about Nokogiri. I don’t think it uses ffi. I use it for my doc site and I don’t even have ffi installed…

1 Like

I’m likely mistaken. I thought the FFI wiki mentioned that Nokogiri used the FFI gem. (But I’m probably confusing with the Racc lib that was recently [and temporarily] removed from the SketchUp distribution.)