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

Ildar Khizbulin <>
Mon Dec 26 10:23:05 CET 2016


Hi,

can I transfer resource from one nif to another nif?

For example in first, i write:

capture.c:

static ErlNifResourceType *f_resource;

capture_f() {
  frame_t* f = enif_alloc_resource(f_resource, sizeof(frame_t));
  f_term = enif_make_resource_binary(env, f, ....
  ret = enif_make_tuple2(env, ok_atom, f_term);
  enif_release_resource(f);
  return ret;
}

load() {
  if(!f_resource)
    f_resource = enif_open_resource_type(env, NULL, "f_res", flags, NULL);
}

in second

hi.c:

static ErlNifResourceType *f_resource;

process_f() {
  struct frame_t *f;
  if (!enif_get_resource(env, argv[0], f_resource, (void **)&f)) { ///<- FAIL


}

load() {
  if(!f_resource)
    f_resource = enif_open_resource_type(env, NULL, "f_res", flags, NULL);
}


logic.erl

  {ok, Bin} = capture:capture_f(),
  hi:process_f(Bin)                    % <- fail


More information about the erlang-questions mailing list