[erlang-questions] NIFs and lists of resources

Jordan Day jordan.day@REDACTED
Wed Sep 19 20:32:13 CEST 2018


Apologies that this is more of a C question than an Erlang question…

I’m working on a NIF accessing a vendor’s C API and wondering what the
correct approach is to handling a function from which I’d like to return a
list of resources. While my functions handling individual resources seem to
be working as expected (calling the destructors when the item is GC’d on
the erlang side), I’m not seeing that happen with a function where I’m
returning a list of resources.

In code where I’d normally do something like
vendor_template templates[num_templates];
fill_templates(templates);
...
return templates;


I’d like each vendor_template struct to become a resource I can pass around
and have GC’d when I’m done with them, on a per-item basis. I presumed I
could do the normal resource-related calls and then return an erlang list,
but I’m not seeing the destructor function called when the list should be
getting GC’d on the erlang side.

Example:

vendor_template *templates = enif_alloc_resource(TEMPLATE_TYPE,
sizeof(vendor_template) * num_templates);
ERL_NIF_TERM template_res[num_templates];
fill_templates(templates);
for (int i = 0; i < num_templates; i++) {
  template_res[i] = enif_make_resource(env, &(templates[i]));
  enif_release_resource(&(templates[i]));
}

ERL_NIF_TERM template_list = enif_make_list_from_array(env, template_res,
num_templates);
return template_list;

The returned list of resources seem to be correct, as I can use their
individual references in subsequent NIF calls, but when the returned list
is discarded, I never see the destructor function called.

Any tips on what I might be doing wrong? Does the
enif_make_list_from_array/2 call “break” this? If so, how can I return my
list of resources?

Regards,
Jordan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180919/9f5c4404/attachment.htm>


More information about the erlang-questions mailing list