[erlang-questions] enif_free_env crashes VM

Jachym Holecek freza@REDACTED
Mon Nov 11 17:43:10 CET 2013


# Volodymyr Kyrychenko 2013-11-11:
> I have big data structure and need to have massively parallel access to 
> it. SO i invented the wheel called references. Here is how it supposed 
> to work - it is NIF copying this BDS into process independent 
> environment and giving processes reference to the term in that env. 

This is not allowed. Excerpt from erl_nif(3):

  A process bound environment is passed as the first argument to all NIFs.
  All function arguments passed to a NIF will belong to that environment.
  The return value from a NIF must also be a term belonging to the same

I'm looking at R14B04 version of the manpage.

> == xl_ref.c =========================================================
> #include <erl_nif.h>
> #include <stdio.h>
> [...]
> ERL_NIF_TERM xl_ref_value(ErlNifEnv* env, int argc, const ERL_NIF_TERM 
> argv[]) {
>     ref_t* ref;
>     if (enif_get_resource(env, argv[0], XL_REF_RESOURCE, (void**)&ref))
>         return ref->value;

This is where the violation happens.

>     else
>         return enif_make_badarg(env);
> }

	-- Jachym

More information about the erlang-questions mailing list