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 restart SketchUp.
Sample code to show the issue:
require 'fiddle'
require 'fiddle/import'
require 'fileutils'
# test
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.
So it seems that during the execution the library is imported only once. It seems that this is the reason why I cannot update it during a SketchUp session.
Any suggestion on how to force Fiddle to reload the library?
I could not find a dlclose method, at least for Fiddle::Importer.
dlload does not return a handle, but it does define, if I understand correctly, a @handler instance variable in the module and this is a Fiddle::CompositeHandler. So I have tried
@handler.handlers.each{|h| h.close}
but I receive the error
#<Fiddle::DLError: dlclose() called too many times>
Also, … I do not believe that the Ruby core is setup to allow #extend to run more than once upon the same object.
Usually these type of mixin methods check the ancestry chain of the object and only are applied if the mixin module does not already appear in the chain.
Anyway you shouldn’t need to extend with the importer mixin again.
You might need to find a way to reinitialize the @handlers object however.