[erlang-questions] weak resource reference possible in nif ?

Skype Xu skypexu@REDACTED
Tue Nov 14 09:11:23 CET 2017


Yes, I have tried OTP 20, it works, also the dirty hack is too expensive 
to run.

On 2017年11月10日 18:27, John Högberg wrote:
> I just thought of a dirty hack that might work; serialize the resource
> on creation with enif_term_to_binary and store *that* in your internal
> list instead of the resource, then send the result of
> enif_binary_to_term in each notification.
>
> The resource won't be kept alive any longer than needed (As the binary
> won't keep any references to it), and it won't crash if the resource
> has been deallocated as enif_binary_to_term will recognize it as such
> and return a stale resource.
>
> It will only work on OTP 20 or later though.
>
> http://erlang.org/doc/man/erl_nif.html#enif_make_resource
>
> /John
>
> On tor, 2017-11-09 at 11:06 +0800, Skype Xu wrote:
>> We don't want to get a new resource term every time when we got a
>> new
>> notification,
>> we want to simply use erlang pattern matching without another
>> function
>> to retrieve
>> actual data in the resource and matching against the data,  if we do
>> this,  a programmer
>> can keep the data around for later matching, and may forget to keep
>> the
>> resource, then
>> what's the reason to use erlang resource? we would give up.
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list