[erlang-questions] Can't get a resource in a nif

Sverker Eriksson <>
Wed Jan 19 11:53:56 CET 2011

Erik Axling wrote:
> Hello!
> I'm trying to write a NIF for the Fast Artificial Neural Network library
> that is written in C. But I'm running into a problem where I use a resource
> to allocate a pointer for a network that I have created. When I try to get
> that resource it fails for an unknown reason.  I have used printfs to be
> able to know where the error occurs. My NIF returns a badarg.
> The C code is here: https://gist.github.com/785854
> Thanks for any help.
> /Erik Axling
Just after a quick glance:

static ERL_NIF_TERM create_standard_nif(ErlNifEnv* env, int argc,
const ERL_NIF_TERM argv[]) {
  int x, ret, tuple_size;
  const ERL_NIF_TERM ** tuple_array;
  unsigned int * converted_array; struct fann * ann; ERL_NIF_TERM result;

ann = enif_alloc_resource(FANN_POINTER, sizeof(struct fann *));
  if(enif_get_tuple(env, argv[0], &tuple_size, tuple_array)) {
    converted_array = malloc(tuple_size*sizeof(unsigned int));
    if(check_and_convert_array(env, tuple_array, tuple_size, 
converted_array)) {
      ann = fann_create_standard_array(tuple_size,
converted_array); You assign the resource pointer to 'ann' and then you 
assign the result of fann_create_standard_array() to 'ann'. Maybe you 
ment to say *ann = fann_create... /Sverker, Erlang/OTP

More information about the erlang-questions mailing list