[erlang-questions] Can i share nif resource between two nif module?

Steve Vinoski <>
Mon Dec 26 22:28:24 CET 2016


On Mon, Dec 26, 2016 at 1:41 PM, Ildar Khizbulin <>
wrote:

> Yep, including. I create simple example on
> https://github.com/hizel/enif_test with full code.
>

What you're trying to do doesn't appear to be supported currently. The
Erlang nif internals require the module name argument (second argument) of
enif_open_resource_type() to be a null pointer (see
http://erlang.org/doc/man/erl_nif.html#enif_open_resource_type). It
generates an atom identifier for the resource type based on the calling
module. You can see that code here:

https://github.com/erlang/otp/blob/maint/erts/emulator/beam/erl_nif.c#L2012-L2013

As a result, when your second module tries to take over the resource type,
the enif_open_resource_type() implementation fails to find the resource you
created in your first module, and so it ends up creating a new resource
type instead. When you try to use this new type in your second module to
retrieve the resource binary argument created by the first module, you get
badarg because the two resource types are different types.

--steve
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20161226/6dceb8b0/attachment.html>


More information about the erlang-questions mailing list