<div dir="ltr"><span style="font-size:12.8px">> This seems to work because you are lucky. :)</span><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Yes, it's crashing randomly after a number of pushes/pops.</span></div><div><span style="font-size:12.8px"><br></span></div><div><div style="font-size:12.8px">> You can think of ErlNifEnv as an array, and ERL_NIF_TERM as an index into that array. The indexes can be copied however you like, but they are only valid on the original array. Your code is storing an index, but is not bringing along the original array. In some cases this will still work (atom terms don't need their environment), or seem to work, but you've planted the seeds of a future mysterious segfault.</div><div style="font-size:12.8px"><span style="font-size:12.8px">> Now you can't keep the ErlNifEnv from your NIF function; you need to use enif_alloc_env() and then copy terms into that ErlNifEnv with enif_make_copy().</span><br></div><div><br></div><div>Thanks for the clarification.<br></div><div><span style="font-size:12.8px"><br></span></div><div class="gmail_extra"><br><div class="gmail_quote">On 12 July 2016 at 15:53, Daniel Goertzen <span dir="ltr"><<a href="mailto:daniel.goertzen@gmail.com" target="_blank">daniel.goertzen@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr">This seems to work because you are lucky. :)<div><br></div><div>You can think of ErlNifEnv as an array, and ERL_NIF_TERM as an index into that array. The indexes can be copied however you like, but they are only valid on the original array. Your code is storing an index, but is not bringing along the original array. In some cases this will still work (atom terms don't need their environment), or seem to work, but you've planted the seeds of a future mysterious segfault.</div><div><br></div><div>Now you can't keep the ErlNifEnv from your NIF function; you need to use enif_alloc_env() and then copy terms into that ErlNifEnv with enif_make_copy().<br><br><br><br><div class="gmail_quote"><div><div class="h5"><div dir="ltr">On Sun, Jul 10, 2016 at 12:17 PM Constantin Kulikov <<a href="mailto:zxnotdead@gmail.com" target="_blank">zxnotdead@gmail.com</a>> wrote:<br></div></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div><div class="h5"><div dir="ltr"><div><div>This seem to work <a href="https://gist.github.com/Bad-ptr/3ea0d6d65ae18b5b68c6cb15a96fef8e" target="_blank">https://gist.github.com/Bad-ptr/3ea0d6d65ae18b5b68c6cb15a96fef8e</a> .</div></div><div><br></div><div>However the documentation says that:</div><div>> All terms of type ERL_NIF_TERM belong to an environment of type ErlNifEnv. The lifetime of a term is controlled by the lifetime of its environment object.<br></div><div><br></div><div>> Variables of type ERL_NIF_TERM can refer to any Erlang term. This is an opaque type and values of it can only by used either as arguments to API functions or as return values from NIFs. 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.<br></div><div><br></div><div>> The environment is only valid in the thread where it was supplied as argument until the NIF returns. It is thus useless and dangerous to store pointers to process bound environments between NIF calls.<br></div><div><br></div><div>So, it is not clear to me is it safe to store an ERL_NIF_TERM(in a form as it passed to a NIF function) in a datastructure created by a NIF function? And does the GC aware that this terms must not be collected ? Etc.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 9 July 2016 at 21:59, Steve Vinoski <span dir="ltr"><<a href="mailto:vinoski@ieee.org" target="_blank">vinoski@ieee.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote"><div><div>On Sat, Jul 9, 2016 at 1:54 PM, Constantin Kulikov <span dir="ltr"><<a href="mailto:zxnotdead@gmail.com" target="_blank">zxnotdead@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr">If I want to add a new datastructure to erlang(my local fork of the erlang-otp from github), what should I do? Yes, I mean a C-level module realization.<div><br></div><div>Let's say I want to add a double linked list(just for simplicity) module with an interface like that:</div><div>dlist:new() -> ?some king of reference to a list?</div><div>dlist:push(item) -> ?reference to a list?</div><div>dlist:pop() -> {item, ?reference to a list?}</div><div>etc...</div><div><br></div><div><div>Is it possible at all? Has anyone ever tried to do something like this?</div></div><div>What definitions to what files should I add? How must I allocate my data structure? How must I reference items in it?<br></div><div>Can someone point me to source locations where erlang's list/ets table/tuple operations(creating a new, storing a reference to objects) defined?</div></div></blockquote><div><br></div></div></div><div>See</div><div><br></div><div><a href="http://erlang.org/doc/tutorial/nif.html" target="_blank">http://erlang.org/doc/tutorial/nif.html</a><br></div><div><a href="http://erlang.org/doc/man/erl_nif.html" target="_blank">http://erlang.org/doc/man/erl_nif.html</a></div><span><font color="#888888"><div><br></div><div>--steve</div></font></span></div></div></div>
</blockquote></div><br></div></div></div><span class="">
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</span></blockquote></div></div></div>
</blockquote></div><br></div></div></div>