[erlang-questions] Regarding the life cycle of ERL_NIF_TERM

Sverker Eriksson sverker.eriksson@REDACTED
Mon Jun 11 15:06:04 CEST 2012

I realize that the documentation might be a bit ambiguous here.
We are actually talking about lifetimes of two different entities:

1. An Erlang term.
2. An ERL_NIF_TERM value as a reference to a term.

For process independent environments both term and reference have the 
same lifetime, that is the same as the lifetime of the environment.

For process bound environments the lifetime of a term is determined by 
the GC. Passing a term as argument to a NIF will not affect the lifetime 
of the term.
The lifetime of an ERL_NIF_TERM reference is however limited to the 
execution scope of the NIF. The reference is only valid until the NIF 
returns. A subsequent call to the same NIF from the same process with 
the same term as argument may result in a different ERL_NIF_TERM value.

/Sverker, Erlang/OTP Ericsson

Xiaopong Tran wrote:
> Hi,
> I have a question regarding the life cycle of ERL_NIF_TERM. According to 
> the Erlang document http://www.erlang.org/doc/man/erl_nif.html:
> "All ERL_NIF_TERM's belong to an environment (ErlNifEnv). A term can not 
> be destructed individually, it is valid until its environment is 
> destructed."
> And the description regarding the ErlNifEnv:
> "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."
> Could someone clarify what that means exactly, in terms of the
> ERL_NIF_TERM life cycle?
> So, in this case, the environment is process bound. Does this
> mean that, all ERL_NIF_TERM passed in a parameters to NIF function,
> and the ERL_NIF_TERM return value, will never get GC, as long
> as the process is running?
> Cheers,
> Xiaopong
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

More information about the erlang-questions mailing list