Libdl.dlopen usage change since v0.7?

ccall
#1

I was trying to convert my codebase from 0.6 to 1.x, but noticed that all pointers from Libdl within a module now appear nulled after compilation. With the following module:

module MyModule
  lib = Libdl.dlopen("/path/to/my/lib.so")
  function foo()
    println(lib)
  end
end

typing the following into the REPL:

using MyModule
MyModule.foo()

yields

Ptr{Void} @0x000000000424bc60
in v0.6

but

Ptr{Nothing} @0x0000000000000000
in v0.7 and beyond

What I’m trying to achieve in my module is to open a library once, call it’s very expensive initialization once and then define a jl-function that can be called many times. But since the handle opened outside the function is nulled inside the function since 0.7, I now seemingly have to dlopen and initialize inside the function, i.e. have the expensive initialization in every function call. How can I avoid this? (The printing of the handle here is just a placeholder for any ccall I might want to make on that handle)

Btw, I’ve noticed that if I define the module inside the REPL and not as a package I still get the old behavior. Not sure why that changes anything.

0 Likes

#2

You’ll need to put that type of dynamic initialization inside your module’s __init__ function. See these docs for details.

0 Likes

#3

Thank you! That’s it, and the code just worked in v0.6 because precompilation is only switched on by default since v0.7.

0 Likes