<div dir="ltr">Thanks for your answer very much. If I understand it correctly this way it should be safe:<div><br></div><div><div>     ErlNifBinary bin;</div><div>     if (!enif_is_binary(env, argv[0]))</div><div>         return enif_make_badarg(env);</div>
<div><br></div><div>     ErlNifEnv *priv_env = enif_alloc_env();<br></div><div>     ERL_NIF_TERM term = enif_make_copy(priv_env, argv[0]);<br></div></div><div><div>     ErlNifBinary bin;</div><div>     enif_inspect_binary(priv_env, term, &bin);</div>
<div>     entry->content.bin = (struct my_bin){.env = priv_env, .size = bin.size, .data = bin.data};</div></div><div><br></div><div>And now mine entry->content.bin.data should be safe but unfortunately it still eventually hangs whole VM after few thousands cycles of referencing this data</div>
<div><br></div><div><div>        CacheEntryRes_t *res = enif_alloc_resource(dc_entry_type, sizeof(CacheEntryRes_t));</div><div>        unless (res) return insufficient_memory(env);</div><div>        res->entry = entry;</div>
<div>        ERL_NIF_TERM result = enif_make_resource_binary(env, res,</div><div>                entry->content.bin.data, entry->content.bin.size);</div><div>        enif_release_resource(res);</div><div>        return result;</div>
<div> </div></div><div>I do mine own reference count and release by</div><div><br></div><div>       enif_free_env(entry->content.bin.env);</div><div><br></div><div>It hangs only if I replace enif_alloc() + memcpy() and enif_free() version with above one :-(</div>
<div><br></div><div>Thanks</div><div>     Hynek</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Aug 29, 2014 at 3:02 PM, Sverker Eriksson <span dir="ltr"><<a href="mailto:sverker.eriksson@ericsson.com" target="_blank">sverker.eriksson@ericsson.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="">On 08/28/2014 03:09 PM, Hynek Vychodil wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi all,<br>
I would like to know if binaries are relocatable. If I have binary parameter<br>
<br>
     ErlNifBinary bin;<br>
     if (!enif_inspect_binary(env, argv[0], &bin))<br>
         return enif_make_badarg(env);<br>
<br>
I store binary in mine own environment<br>
<br>
     ErlNifEnv *priv_env = enif_alloc_env();<br>
     enif_make_copy(priv_env, argv[0]);<br>
<br>
Can I then rely on bin.data pointing into same memory until I free<br>
enif_free_env(priv_env) ?<br>
</blockquote></div>
No.<br>
Your bin.data is refering to the original term argv[0] in 'env' which is only valid until the NIF returns.<br>
You must do enif_inspect_binary on the copy in order to get a safe pointer that is valid until you do enif_free_env(priv_env).<div class=""><br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  I'm asking because I would like to avoid copying<br>
and share data or even return it using enif_make_resource_binary to catch<br>
and<br>
count references.<br>
</blockquote></div>
Whether a copied binary share data with its original is implementation dependent. Today big binaries (>64 bytes) are shared<br>
and small binaries are copied between environments.<div class=""><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I have code which makes copy of binary data but when I switch to reference<br>
bin.data it hangs whole VM. I would like to know if I make some mistake in<br>
mine code and it should work or it is wrong idea at all.<br>
<br>
<br>
</blockquote></div>
There is a known bug when calling enif_make_copy after enif_inspect_binary<br>
that can cause the binary to get reallocated and thus the bin.data pointer to get invalid<br>
before the NIF returns. To detect this bug, you can call enif_inspect_binary(env,argv[<u></u>0],&bin2)<br>
after enif_make_copy and check if bin2.data != bin.data.<br>
<br>
This bug has been lingering as I thought it was an exotic scenario (apparently not) and I have<br>
been a bit unsure how to fix it without ruin performance.<br>
<br>
<br>
/Sverker, Erlang/OTP<br>
<br>
</blockquote></div><br></div>