[erlang-questions] Sharing resource types between NIFs

Loïc Hoguin <>
Mon Feb 17 15:19:46 CET 2014

On 02/17/2014 02:56 PM, Sverker Eriksson wrote:
> Having dependences between NIF modules (or drivers) on the native code
> level is not supported.
> How symbols are resolved (or not) between dynamic libraries is OS
> dependent (Windows).
> The runtime system can not keep track of such dependencies. For example,
> if module B makes a native call to a function in module A and module A
> has been unloaded...VM crash (or worse).
> I suggest you merge your dependent NIF code into one module. You can
> still have different modules on Erlang level and have them call a
> backend NIF module.

Thanks, that's exactly what I ended up doing.

> That being said... ... the scenario you describe should work in current
> implementation. enif_get_resource() does not care where it is called from.

Well it didn't work. :-)

> /Sverker, Erlang/OTP
> On 02/15/2014 11:22 PM, Loïc Hoguin wrote:
>> Hello,
>> I have been looking into this and after looking into the source it
>> doesn't seem possible, but I might as well ask just in case.
>> Say I am writing bindings for a big library. That library is cut into
>> various components, and I would like to do similar on the Erlang side.
>> Components are more or less dependent. Let's just say that component B
>> uses component A. You can't do anything in B without A. They are both
>> NIF resources pointing to things used by the library.
>> Now if I try to create a NIF module for A and a NIF module for B, I
>> need to access the resource type of A from the B NIF. I tried simply
>> exporting a function from the A .so and use it from the B .so, the
>> function returns the resource type as expected, however using it to
>> retrieve the resource fails.
>> Trying to do the same from A of course works. It's just trying to use
>> the resource type of A from B that fails.
>> I'm assuming this isn't possible?
>> Thanks.

Loïc Hoguin

More information about the erlang-questions mailing list