Rojj
Rojj

Reputation: 1210

Ruby Fiddle - Reload dynamic library

I am using Fiddle to load a function from a dynamic library written in C. During development I need to make changes to the library and reload it for testing. The problem is that dlload does not reload the library unless I relaunch the script. I am developing a plugin in Ruby for SketchUp so "relaunching" the script would actually mean to restart the application.

Sample code to show the issue:

require 'fiddle'
require 'fiddle/import'
require 'fileutils'

module RG
  extend Fiddle::Importer  
  dlload 'utils.dylib'
end

FileUtils.rm 'utils.dylib'

module RG
  extend Fiddle::Importer  
  dlload 'utils.dylib'
end

If you launch the script the first time there is no error. If you launch it the second time you get the error: image not found, as expected.

So it seems that during the execution the library is imported only once. Any suggestion on how to force Fiddle to reload the library?

I am using macos and Ruby 2.0

Upvotes: 0

Views: 196

Answers (1)

Rojj
Rojj

Reputation: 1210

Just in case someone needs this in the future. You can use

@handler.handlers.each {|h| h.close unless h.close_enabled? } unless @handler.nil?
GC.start

So you first close all the handlers and then force the garbage collection.

Upvotes: 0

Related Questions