<div dir="ltr">This is exactly the weak reference/weak pointer pattern.  I don't see any quick shortcuts for you here.<div><br></div><div>If you are using c++, your resources can represent shared_ptr and your stored static variable can be a weak_ptr.  Rust has similar types.  Unfortunately you would also need to write a NIF for comparing the underlying referred object.</div><div><br></div><div><br></div><div><br></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Dec 19, 2017 at 9:03 AM Loïc Hoguin <<a href="mailto:essen@ninenines.eu">essen@ninenines.eu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
I've got the following situation.<br>
<br>
I have a function SDL_CreateWindow that returns an SDL_Window* from<br>
which I create a resource with a destructor which is called when the<br>
resource variable gets GC'd. So far so good.<br>
<br>
I also have various other functions which may return the same<br>
SDL_Window* as the one that was created earlier. In this case I want to<br>
return the same resource so that it is possible to match or compare the<br>
resources on the latest Erlang versions so that users can confirm that<br>
yes, it's the same window. This is useful to know for example to know<br>
which window currently has focus.<br>
<br>
I therefore need to keep the ERL_NIF_TERM around along with SDL_Window*<br>
since recreating the term would lead to different references and<br>
comparisons failing. I've therefore proceded to use enif_make_copy to<br>
copy it to a static variable and I do enif_make_copy again when I need<br>
to return it.<br>
<br>
It works.<br>
<br>
Almost.<br>
<br>
The only issue with my approach is that the destructor for the<br>
SDL_Window* resource no longer gets called. Apparently enif_make_copy is<br>
the culprit. As soon as I copy the resource term to my private env, the<br>
destructor stops being called.<br>
<br>
Is there any way I can keep this resource term while still benefitting<br>
from the destructor being called when the variable is discarded?<br>
Basically I would like the private copy to not be reference counted. Any<br>
way to do this?<br>
<br>
Thanks,<br>
<br>
--<br>
Loïc Hoguin<br>
<a href="https://ninenines.eu" rel="noreferrer" target="_blank">https://ninenines.eu</a><br>
_______________________________________________<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>
</blockquote></div></div>